Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

Tabs to spaces converter

A tabs to spaces converter swaps each tab character for the spaces that fill the same room. The five-line sample on this page has 6 tabs in 4 lines and grows from 78 to 94 characters at a tab width of 4.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

How a tab is replaced
Which tabs
Text with spaces
def total(items): sum = 0 for i in items: sum += i.price # add return sum
  • 6 tabs replaced
Tabs replaced
6
Lines changed
4 of 5
Tabs left alone
0
Characters
78 before, 94 after

How to use the Tabs to Spaces Converter

  1. Paste your code or text into Text with tabs. The sample is replaced as soon as you type or paste.
  2. Set the Tab width, a whole number from 1 to 16. Use the width your editor or style guide shows, often 2, 4 or 8.
  3. Choose How a tab is replaced: up to the next tab stop, or always the full width. Tick Indentation only to leave tabs that sit inside a line.
  4. Check the counts under the result, then press Copy and paste the text back into your file.

How do you convert tabs to spaces?

Each tab has to become some number of spaces, and there are two ways to pick that number. The first is the tab stop method. A tab jumps to the next column that is a multiple of the tab width, so the number of spaces depends on where the tab sits. At width 4, a tab after one character becomes 3 spaces, and a tab at the start of a line becomes 4. This is how editors draw tabs and how Python's expandtabs works, so it keeps columns lined up.

The second method is fixed width: every tab becomes exactly the width in spaces, wherever it is. That suits pure indentation, where every tab sits at the start of a line and the two methods agree. For text with tabs in the middle of a line, the results differ. The line a[tab]b becomes a b by stops and a b by fixed width at width 4.

The sample on the page shows the stop method at width 4. Its first tab sits at column 0 and becomes 4 spaces. The tab after sum += i.price sits at column 22, so it needs only 2 spaces to reach column 24, and the comment lines up with the next stop. The result has 94 characters, 16 more than the 78 you started with.

Which tab width should I pick?

Use the width the file was written with. A file indented with tabs shows its structure at whatever width the editor is set to, so the converter has to match that setting or the nesting will look wider or narrower than before. If you do not know it, try 4 first and compare the line lengths. Width changes both the look and the size of the text, as the table shows for the sample.

The columns in the table are the length with stops, the length with fixed width, and the extra characters the stop method adds. The two methods differ in the sample by 2 characters at width 4, because only the tab inside the line is affected.

Length of the sample after conversion, from the same function as the tool
Tab widthCharacters (stops)Characters (fixed)Added by stops
284846
4949616
811412036

How do tab stops keep columns lined up?

Take the text a[tab]bc[tab]def[tab]g. With stops, each tab fills the gap to the next multiple of the width, so the second letters of a column line up wherever the words end. The table shows the result with a dot for each space. At width 4 the text becomes a···bc··def·g, with 3, 2 and 1 spaces: each tab fills only what is left to the next stop.

This only works because the converter counts columns from the start of each line and resets the count at every line break, including Windows line endings (CRLF) and old Mac ones (CR). The line endings in your text are kept as they were. Columns count characters, so an emoji counts as one column, and wide characters from some scripts may be drawn wider in your editor than the count assumes.

Turn on Indentation only when a file has tabs that are part of the content, such as tab separated values inside a line. With the option on, the sample at width 4 changes 5 tabs and leaves 1 in place, because that one tab follows code instead of starting the line. Makefiles need a real tab at the start of each recipe line, and Indentation only still replaces it, so keep Makefiles out of this page. The remove extra spaces tool does something different: it turns each tab into a single space. For converting whole data tables, the CSV to JSON converter reads comma separated text.

The text a[tab]bc[tab]def[tab]g with a dot for each space
Tab widthResult
2a·bc··def·g
4a···bc··def·g
8a·······bc······def·····g

Use it with

Questions

How many spaces is a tab?

It depends on the tab width set in the editor, often 2, 4 or 8. Set the same number here. By the stop method a tab can be fewer spaces than the width when it follows text on the line.

What is the difference between tab stops and fixed width?

Tab stops move to the next multiple of the width, so the spaces added vary with the column. Fixed width always adds the full width. They match for tabs at the start of a line and differ for tabs inside a line.

Does it change the line endings?

No. Windows, old Mac and Unix line breaks are kept exactly as they were, and a trailing newline stays.

Can I convert only the indentation?

Yes. Tick Indentation only, and tabs after the first non-space character of a line stay as tabs.

Is my text sent anywhere?

No. The conversion is plain string handling in this page, so the text never reaches a server and nothing is stored.

Can it turn spaces back into tabs?

No, this page goes one way, from tabs to spaces.

The tool's results are computed in your browser by the code on this page; the explanations are checked against the tool's own output.