Tabs vs Spaces: How Tab Stops Work and How to Convert Between Them

Understand tab stops, why mixed tabs and spaces break alignment and code, and how to convert tabs to spaces or back in a browser, editor or terminal.

Updated September 29, 2026 6 min read

Use the free Tabs to Spaces ConverterConvert tabs to spaces or spaces to tabs with real tab stops

Few formatting questions have generated as much debate among programmers as tabs versus spaces. Behind the jokes there are real, practical problems: code that looks perfectly aligned in one editor and ragged in another, Python scripts that refuse to run because of "inconsistent use of tabs and spaces", YAML files that fail to parse, and tab-separated data that loses its columns when someone pastes it into a document.

This guide explains how tabs actually work, why a correct conversion is more than "replace every tab with four spaces", how to convert in either direction, and which conventions common languages and formats expect.

A tab is not a fixed number of spaces

A tab character does not mean "insert N spaces". It means "move to the next tab stop". Tab stops are positions set at regular intervals, most commonly every 4 or 8 columns. So the amount of space a tab produces depends on where it starts.

With tab stops every 4 columns:

ab<TAB>c     ->  "ab" occupies columns 0-1, the tab jumps to column 4
abc<TAB>d    ->  "abc" occupies columns 0-2, the tab jumps to column 4
<TAB>x       ->  the tab jumps from column 0 to column 4

In the first line, the tab is worth two spaces; in the second, one; in the third, four. A tool that blindly replaces every tab with four spaces would misalign the first two lines, which is exactly how tab-separated tables fall apart. A correct conversion, often called expanding tabs, calculates the distance to the next tab stop for each tab.

The reverse, unexpanding, replaces runs of spaces that reach a tab stop with a tab. A single space before a tab stop is usually left as a space, since a tab there would save nothing.

Why mixed indentation causes problems

  • Different editors, different widths. One developer sets tab width to 4, another to 8, a code review tool uses 8, and a web page uses whatever the browser defaults to. Code indented with a mix of tabs and spaces lines up in only one of those settings.
  • Languages where indentation is meaningful. Python uses indentation to define blocks. Mixing tabs and spaces in the same block raises a TabError in Python 3.
  • Formats that forbid tabs. YAML does not allow tab characters for indentation, so a single tab can make a configuration file invalid.
  • Formats that need tabs. Makefiles require recipe lines to start with a real tab character, so converting them to spaces breaks the build.

The fix is consistency: choose tabs or spaces for each file type, and convert anything that does not follow the rule.

Convert tabs and spaces step by step

The Tabs to Spaces Converter uses real tab stops in both directions.

  1. Open the Tabs to Spaces Converter and paste your code or text into Input. Click Sample to see a short function and a small tab-separated table.
  2. Choose the Direction: Tabs → Spaces or Spaces → Tabs.
  3. Pick the Tab width: 2, 3, 4 or 8 spaces. Use the width the text was written with; if you are unsure, 4 is common in code and 8 is the traditional terminal default.
  4. Switch on Leading indentation only if you want to change indentation but leave tabs or spaces inside lines untouched, for example to keep aligned comments or a tab-separated column as it is.
  5. Check the output. The summary tells you how many tabs were expanded or inserted. Copy the result or click Download.

Common conventions

Style guides and tools differ, so treat this table as a starting point and check your project's own settings:

Language or format Usual convention
Python Spaces, 4 per level (PEP 8)
JavaScript / TypeScript Mostly spaces, 2 or 4, set by the project's formatter
Go Tabs (enforced by gofmt)
YAML Spaces only; tabs are not allowed for indentation
Makefile Recipe lines must start with a tab
TSV data Tabs between columns, by definition

Other ways to convert

In a code editor

  • VS Code: the status bar shows "Spaces: 4" or "Tab Size: 4". Click it to choose Indent Using Spaces or Indent Using Tabs, then use Convert Indentation to Spaces or Convert Indentation to Tabs from the Command Palette. These commands change indentation only.
  • Notepad++: the Edit → Blank Operations menu has options to convert tabs to spaces and spaces to tabs.
  • Most IDEs have a "reformat" command that re-indents a file according to project settings, which is the best option for whole projects.

An .editorconfig file in the project root lets you define indentation style and size once, so that many editors apply the same rules automatically.

On the command line

Unix-like systems include two dedicated utilities:

expand -t 4 input.txt > output.txt       # tabs to spaces, tab stops every 4
unexpand -t 4 --first-only input.txt      # leading spaces back to tabs

These are ideal for many files or for build scripts. Without options, unexpand converts only leading whitespace; -a converts all runs of spaces. In GNU coreutils, setting -t also switches on -a, which is why --first-only is added above.

With a formatter

For code, automatic formatters such as Prettier, Black or gofmt are the most reliable long-term solution, because they fix indentation along with everything else on every save. For individual snippets, our JavaScript Formatter re-indents code with an indent size or tabs of your choice, and the JSON Formatter re-indents JSON with two spaces per level.

Tips and troubleshooting

Aligned comments shifted after conversion

Tabs inside a line depend on the column where they start. If the original was written with a different tab width than the one you chose, alignment changes. Try other widths, or turn on Leading indentation only.

Converting back does not give the original

Spaces-to-tabs conversion creates tabs only where runs of spaces reach a tab stop. If the original mixed tabs and spaces unevenly, the round trip produces a cleaner but not identical result.

Tab-separated data stopped working

If a program expects TSV, converting tabs to spaces destroys the column separators. Keep the tabs, or convert the data to CSV with a spreadsheet instead.

Trailing whitespace remains

Converting tabs does not remove spaces at the ends of lines. Use Remove Extra Spaces or the trailing-space option in the Text Cleaner.

Frequently asked questions

How many spaces is a tab?

There is no fixed number. A tab moves to the next tab stop, and stops are usually every 4 or 8 columns, so a tab can be worth anywhere from one space up to the full tab width.

Why does Python say "inconsistent use of tabs and spaces"?

Python 3 raises a TabError when indentation in a block mixes tabs and spaces in a way that depends on tab width. Convert the file to spaces only, typically four per level.

Can YAML files use tabs?

Not for indentation. The YAML specification forbids tab characters for indentation, so convert them to spaces.

Should Makefiles use spaces?

No. Recipe lines in a Makefile must begin with a tab character, so leave Makefiles out of any tabs-to-spaces conversion.

What does "leading indentation only" do?

It converts only the whitespace at the start of each line and leaves tabs or spaces after the first visible character unchanged, which protects aligned comments and tab-separated columns.

Try it now: Tabs to Spaces Converter

Loading tool…