Tabs to spaces converter

Convert tabs to spaces or spaces back to tabs using real tab stops (2, 3, 4 or 8), on the indentation only or on the whole line, with tabs made

loading Tabs to spaces converter
About this tool

What does a tabs to spaces converter do?

A tab character is not a fixed number of spaces. It moves the cursor to the next tab stop, which is a multiple of the tab width: with a width of 4, a tab typed at column 0 spans four columns, but a tab typed after the letters "ab" spans only two. That is why code indented with tabs looks different in different editors, and why replacing every tab with four spaces by hand breaks alignment as soon as a tab is not at the start of the line.

This tool converts in both directions with real tab stops, the same arithmetic as the expand and unexpand commands of Unix systems. "Tabs to spaces" replaces each tab with the spaces that reach the next stop. "Spaces to tabs" packs runs of spaces back into tabs wherever they fill a stop. You choose the tab width (2, 3, 4 or 8), and whether to touch only the indentation at the start of each line or the whole line. A switch shows every tab in the result as an arrow so you can see what is a tab and what is a space, and the status line counts how many tabs there were before and after.

Everything happens in your browser. The text is not uploaded or stored; only your choices, such as the width, are remembered.

How to convert indentation

  1. Paste your code or text. The page opens with a small function indented with tabs, so you can try the options at once.
  2. Pick the direction: "Tabs to spaces" to expand, "Spaces to tabs" to pack.
  3. Set the tab width to the one your project uses. Two and four are the most common for code; eight is the traditional width of terminals.
  4. Choose "Indentation only" to change just the leading whitespace, which keeps tabs used for alignment in the middle of a line (a comment after code, a column in a table). Choose "Whole line" to convert every tab.
  5. Turn on "Show tabs as →" to check the result, then copy it or download it as a text file.

Examples

Expanding code indented with tabs

Input (each indentation step is one tab):

if (ready) {
→	run();
}

With width 4 and "Tabs to spaces", the arrow line becomes four spaces plus run();:

if (ready) {
    run();
}

A tab after text

The line ab<TAB>c with width 4 becomes ab followed by two spaces and c, because the tab only had to reach column 4. A converter that always writes four spaces would give ab c and push the c two columns too far.

Packing spaces into tabs

Eight spaces at the start of a line with width 4 become two tabs. Six spaces become one tab and two spaces, because only four of them fill a stop. A single space is never turned into a tab, since it would save nothing.

Mixed indentation

A line that starts with two spaces and then a tab reaches column 4 with a width of 4, not column 6. The status below the tool counts how many lines mix tabs and spaces in their indentation, which is usually the reason a file looks misaligned in someone else's editor.

Common mistakes

  • Choosing the wrong width. If the result looks too shallow or too deep, your project probably uses a different tab width than the one selected. Check the editor settings or the style guide.
  • Converting the whole line in code with aligned comments. Tabs in the middle of a line are often alignment. With "Whole line" they become spaces that match today's width, and a later change of width will not realign them. Use "Indentation only" if you want them untouched.
  • Expecting Makefiles to keep working. A Makefile requires a real tab at the start of each recipe line. Do not convert tabs to spaces in a Makefile.
  • Copying the arrows. The arrows are only a view. Copy and download always give the real characters.
  • Pasting from a page that already turned tabs into spaces. Many sites and chat apps replace tabs on paste. If the text has no tabs when it arrives, there is nothing to expand.

Frequently asked questions

Is this the same as the expand and unexpand commands?

Yes for the arithmetic: the results are checked against the system expand and unexpand for widths 2, 3, 4 and 8. The "Indentation only" scope is an addition that those commands apply only in some versions.

Does it handle accented letters and emoji?

Columns are counted by character, so accented letters count as one column each. Wide characters such as Chinese ideograms or some emoji count as one, as in the system tools, even if your editor draws them wider.

Why does the line count not change?

Only whitespace inside a line is changed. Line breaks, including Windows line endings, are kept exactly as they are.

Can it detect which width my file uses?

Not automatically, because a file with tabs does not record a width. The count of lines that mix tabs and spaces helps you see where the indentation is inconsistent.

What should I do after converting?

Compare the result with the original using the text comparison to see that only whitespace changed, or reformat the code with the code beautifier. For sorting or filtering lines, use the line tools.

Report a problem