HomeFormattersTextTab to Space Converter

Tab to Space Converter

Text

Replace tabs with spaces in any code or text snippet. Choose 2, 4, or 8 spaces per tab — runs entirely in your browser with no data uploaded anywhere.

What is a Tab/Space?

The Tab to Space Converter replaces every tab character in a block of code or text with a configurable number of spaces — 2, 4, or 8 — producing output with consistent space-based indentation. It is one of the most common code-cleanup operations, needed whenever code from one project or editor needs to conform to a different indentation standard.

Tab characters are invisible but consequential. In an editor set to display tabs as 4 spaces, the code looks neatly aligned. In another editor set to 8 spaces, the same file looks wildly over-indented. When the file is displayed in a terminal, a code review tool, or a web-based editor with its own tab width setting, the alignment can look completely different from what the author intended.

Replacing tabs with spaces removes this ambiguity. A file with 4-space indentation looks the same in every tool, at every font size, in every terminal — because spaces render as exactly one character-width each. This is why most modern style guides (PEP 8 for Python, the Google style guides, Airbnb's JavaScript style) mandate spaces over tabs.

All conversion runs entirely in your browser. No code or text you paste is sent to a server or stored anywhere. Use the Line Break Remover if you also need to clean up extra blank lines or normalise line endings in the same snippet.

How to use this Tab/Space calculator

  1. Paste your code or text into the Input Text box — any text containing tab characters (\t) will be converted.
  2. Choose the number of spaces to substitute for each tab from the Spaces Per Tab dropdown: 2, 4, or 8.
  3. The Converted Text output updates instantly as you change either the input or the spaces setting.
  4. Review the output to confirm the indentation looks as expected.
  5. Click the Copy button to copy the result and paste it back into your editor, terminal, or file.

Formula & Methodology

The conversion is a direct string replacement: every tab character (\t) in the input is replaced with the chosen number of space characters ( ).

output = input.split('\t').join(' '.repeat(spacesPerTab))

This is a non-recursive, single-pass operation — each tab is replaced exactly once, regardless of where it appears in the string. Existing space characters are untouched.

Before (4 spaces per tab):
function greet(name) { →console.log('Hello, ' + name); →if (name) { →→return true; →} }
(where  represents a tab character)

After:
function greet(name) {     console.log('Hello, ' + name);     if (name) {         return true;     } }
Frequently Asked Questions
What does a tab to space converter do?
A tab to space converter replaces every tab character (` `) in your text or code with a configurable number of spaces. This is useful when a file was created with tab indentation but needs to conform to a style guide that requires spaces, or when pasting code into a system that renders tabs at an unexpected width. The Tab to Space Converter on thecalcu.com supports 2, 4, and 8 spaces per tab.
Why do tabs and spaces matter in code?
Most code editors allow mixing tabs and spaces for indentation, but doing so produces inconsistent visual alignment across editors and tools that render tabs at different widths. Python, in particular, will raise an IndentationError if tabs and spaces are mixed. Many open-source projects enforce spaces-only indentation via linters like ESLint, Prettier, or Black, so converting tabs to spaces is a common step when contributing to or importing from a different codebase.
How many spaces should I use per tab?
The right number depends on your project's style guide. Two spaces per tab is common in JavaScript and TypeScript projects (including React), four spaces per tab is the standard for Python (PEP 8) and many Java and C# projects, and eight spaces is traditional in older Unix tools and some C projects. When in doubt, check the `.editorconfig` or linter configuration in your project's root directory.
Can I convert spaces back to tabs?
The current tool converts tabs to spaces only. For converting spaces back to tabs, you can use your code editor's built-in command — in VS Code, open the Command Palette and run 'Indent Using Tabs' — or use a dedicated editor plugin.
How do I convert tabs to spaces in my code?
Paste your code into the Input Text box, select the number of spaces per tab from the Spaces Per Tab dropdown (2, 4, or 8), and the converted output appears instantly. Click the Copy button to copy the result, then paste it back into your editor or file.
Does this affect spaces already in the text?
No. The converter replaces only actual tab characters (` `) with the chosen number of spaces. Existing space characters in the text are left completely unchanged. This means lines that were already space-indented retain their original indentation exactly.
Does this work with mixed tab and space indentation?
Yes. Every tab character in the input is replaced, regardless of whether the same line also contains spaces. The result may still have inconsistent alignment if the source was already mixed, but all tab characters will be gone. For a fully normalised result, you may need to reformat the code with a dedicated tool like Prettier afterward.
Is my code uploaded anywhere?
No. All conversion happens entirely in your browser. The code or text you paste is never sent to any server, stored, or shared. You can safely paste proprietary source code, internal scripts, or confidential data.
Does this work offline?
Yes — once the page has loaded, the formatter runs without a network connection. The conversion is a pure JavaScript string operation that requires no server communication.
Can I use this for non-code text?
Yes. The converter works on any text containing tab characters, including data files, configuration files, log outputs, and copy-pasted spreadsheet data where tabs are used as column separators. Note that converting tabs in a TSV (tab-separated values) file will break the column structure, so this is best used for indentation rather than data separators.
What is the difference between hard tabs and soft tabs?
Hard tabs are actual tab characters (` `) stored in the file. Soft tabs are sequences of spaces that look like a tab but are stored as individual space characters. Most modern editors can be configured to insert soft tabs (spaces) automatically when you press the Tab key. A tab-to-space converter turns hard tabs into soft tabs at a specific width, making the file display consistently regardless of the editor's tab-width setting.