Tab ↔ Space Converter

Expand tabs to spaces or compress spaces to tabs at a chosen width

Ad placeholder (leaderboard)

Inconsistent indentation — a mix of tabs and spaces, or the wrong tab width — is one of the most common causes of noisy diffs and broken layout. This tool converts cleanly in both directions while respecting how tabs actually behave.

How it works

A tab character does not equal a fixed number of spaces; it advances the cursor to the next tab stop, which is the next column that is a multiple of the tab width. Expansion tracks the current column per line and pads only as far as the next stop:

next_stop = floor(column / width) * width + width
spaces    = next_stop - column

Compressing back to tabs works only on the leading indentation. The indent is first expanded to spaces, then every full group of width spaces becomes one tab, with any remainder left as spaces.

Tips and notes

  • Set the width to match your editor: 4 is common for many languages, 8 for C and Go-style tabs, 2 for some JavaScript styles.
  • Use tabs-to-spaces before pasting into tools that render tabs unpredictably, such as web forms or chat.
  • The compress direction deliberately ignores spaces inside a line so that carefully aligned columns are not destroyed.
Ad placeholder (rectangle)