Converting between tabs and spaces
Code pasted from a website, an old project or a colleague's editor often comes with the wrong indentation: tabs where your project uses spaces, or the other way round. Mixed indentation breaks Python and YAML outright and makes diffs noisy everywhere else.
This tool converts leading indentation in either direction with the tab width you choose. Only the indentation at the start of each line changes; tabs and runs of spaces inside a line stay untouched.
How to use it
- Paste your text or code into the input box.
- Choose the direction: tabs to spaces, or spaces to tabs.
- Set the tab width, typically 2 or 4.
- Copy the converted text from the output box.
Example: tabs to four spaces
if (x) {
return 1;
} if (x) {
return 1;
}Each leading tab becomes four spaces, so the nesting is preserved exactly.
How spaces become tabs
When converting spaces to tabs, every full group of spaces as wide as the tab width becomes one tab. Spaces left over at the end of the indentation stay as spaces: with a width of 2, five spaces become two tabs and one space. That keeps alignment that was not a whole indent level, such as continuation lines.
Tabs or spaces?
Both work, and consistency matters more than the choice. Spaces look identical in every editor and on every website, which is why most style guides, including PEP 8 for Python and the common JavaScript ones, use them. Tabs let each reader choose their own indent width, which helps people who need larger indents for readability, and they are required in Makefiles. Go's gofmt uses tabs.
Make it stick
After a one-off conversion, set the project's preference in an .editorconfig file or your formatter's configuration so the problem does not come back. Committing a whole-file indentation change separately from real code changes keeps the history readable.
Questions
Will it change tabs inside strings?
No. Only indentation at the very start of a line is converted.
Can it fix mixed tabs and spaces on one line?
Convert tabs to spaces first; the result is consistent spaces, which you can then convert to tabs if needed.
Is my code uploaded?
No. Conversion runs in your browser.