To change indentation from tabs to spaces, or the other way around, you rewrite the characters at the start of each line and decide how many columns a tab takes up. The tabs to spaces converter does it in one step.
You paste a code snippet from a source that uses tabs into a project that uses spaces (or the other way around), and suddenly the indentation looks misaligned in your editor, even though it looked right in the original. The cause is that tabs and spaces are different characters, and each editor can display a tab at a different width.
Why mixing tabs and spaces breaks visual indentation
A tab has no fixed width: each editor, or each setting within the same editor, decides how many columns it takes. If a file mixes lines indented with tabs and lines indented with spaces, it only lines up in an editor that uses the same settings as the original author. That's the classic "it looks fine on my editor, but not on yours" problem.
How to convert your code's indentation
- Paste the code you want to convert.
- Choose the direction: tabs to spaces, or spaces to tabs.
- Set the width: how many columns one tab is worth. The tool offers 2, 3, 4 and 8, and any value from 1 to 64.
- Copy the result.
The tool only changes the spaces and tabs at the start of each line; the rest of the text stays as it is. When converting tabs to spaces, each tab advances to the next multiple of the width: with a width of 4, two spaces followed by a tab give 4 spaces, not 6. When converting spaces to tabs, each group of spaces equal to the width becomes one tab, and the leftover spaces stay as spaces. Existing tabs aren't touched in that direction.
Tabs vs. spaces: why it's still a debate
Both sides have real arguments:
- Tabs: each developer can use the width they prefer without changing the file, because a tab stands for one indentation level, whatever number of characters it takes.
- Spaces: the code looks the same in any editor, without depending on each one's settings, and it's harder to mix the two by accident.
Python's style guide, PEP 8, says spaces are the preferred indentation method, and that tabs should only be used to stay consistent with code that is already indented with tabs. Other languages and projects may follow different conventions, so the final choice depends on what your project sets.
When you need to convert indentation
- When integrating code from different sources (an external library, a Stack Overflow snippet, a colleague's code) that uses a different convention than your project.
- When migrating a project to a style guide that requires a specific indentation format.
- When debugging huge diffs in version control caused by an accidental switch from tabs to spaces (or the other way around) in a file. Git marks every line as changed even though the logical content hasn't changed. To review what changed, compare the two versions with the text comparison tool or read how to compare two texts.
Frequently asked questions
Does converting indentation change how the code behaves? In most languages, no. There are three exceptions worth knowing:
- In Python, indentation is part of the syntax. If a file mixes tabs and spaces in a way that the meaning depends on the width of a tab, Python raises a
TabError(language reference). - In a Makefile, every recipe line must start with a tab, unless you change
.RECIPEPREFIX(GNU make manual). Turning those lines into spaces breaks the file. - Inside a multi-line string literal, the tool also changes the spaces at the start of each line, so the string's content changes.
Which width should I pick? The one your project or your language's style guide sets. The tool offers 2, 3, 4 and 8, and any value from 1 to 64.
Can I convert in both directions? Yes, from tabs to spaces and from spaces to tabs.
Is my code uploaded to any server? No. The whole process happens in your browser.
Convert your code's indentation between tabs and spaces for free with the tabs to spaces converter, with configurable width.