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.

Reviewed by the thecalcu.com team · Last updated June 23, 2026

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.

Why Use a Tab to Space Converter?

The most common scenario is contributing to a project with a different indentation style than the one you normally use. If your editor inserts hard tabs and the project enforces spaces via a linter or .editorconfig, every file you submit will fail the style check. Running your code through the converter before committing resolves the issue instantly.

A second common scenario is pasting code from an external source, Stack Overflow, a documentation page, a colleague's email, where tab characters may have been preserved from the original. These tabs can cause Python indentation errors or break the visual alignment of the surrounding code.

Data analysts encounter the same issue when opening TSV or log files in text editors: tab characters that were meant as column separators produce unexpected visual indentation when the file is viewed as code rather than data.

Who Should Use This Formatter?

Developers switching between projects with different indentation conventions, tabs in one, spaces in another, need a quick way to normalise snippets they copy across projects. The Tab to Space Converter is faster than configuring your editor's re-indentation command for a one-off snippet.

Open-source contributors submitting code to projects that enforce PEP 8, Google style, or Prettier will find this tool useful for cleaning up snippets before creating a pull request, particularly when the contribution is a small patch copied from a personal project with different settings.

Students and learners copying code examples from textbooks or PDFs, where the PDF-to-text conversion may have introduced tab characters, can use this formatter to clean up examples before running them. Combine it with the JSON Formatter when working with data examples.

Technical writers producing code samples for documentation need consistent space-based indentation so that code blocks render identically in Markdown, HTML, and publishing platforms, none of which respect tab-width settings.

What Insights Does the Tab to Space Converter Give You?

The output directly reflects the indentation depth of the original code. Selecting 8 spaces per tab makes deeply nested code visually obvious, blocks indented 4 levels wide become 32 spaces and stand out immediately as candidates for refactoring. Selecting 2 spaces per tab is useful for compacting deeply nested code for display in a narrow column, such as a blog post or documentation sidebar.

Spaces per tab Typical use
2 JavaScript/TypeScript (Airbnb, Google style), YAML, HTML templates
4 Python (PEP 8), Java, C#, PHP
8 Traditional Unix C code, GNU project style, some shell scripts

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

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.
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.
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.
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.
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.
It only replaces 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.
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.
Nothing is sent to a server. 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.
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.
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.
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.
Also known as
tabs to spacesspaces to tabsindentation convertertab space formatterconvert tabs to spaces