Indentation converter
What is an Indentation Converter?
Teams and languages disagree about indentation: Python's PEP 8 asks for 4 spaces, most JavaScript and YAML projects use 2, Go uses tabs, and Makefiles require tabs. Code copied between projects or written in different editors often ends up with mixed indentation, which breaks Python and YAML and makes diffs noisy. This tool converts leading indentation consistently.
How to use it
- Paste your code or text.
- Choose the source indentation (tabs or a number of spaces) and the target.
- Copy the converted result.
Example
Converting 4-space indentation to 2 spaces:
function greet(name) {
if (name) {
return 'Hi ' + name;
}
}becomes:
function greet(name) {
if (name) {
return 'Hi ' + name;
}
}Only leading whitespace changes; spaces inside lines and string contents are untouched.
Where indentation matters
- Python: indentation defines blocks. Mixing tabs and spaces raises
TabErrorin Python 3. - YAML: tabs are not allowed for indentation at all, and nesting depth must be consistent.
- Makefiles: recipe lines must start with a tab, or make reports missing separator.
Preventing the problem
- Add an
.editorconfigfile withindent_styleandindent_sizeso every editor follows the same rules. - Use a formatter such as Prettier, Black or gofmt in your editor and CI.
- Enable visible whitespace in your editor to spot stray tabs.
Frequently Asked Questions
Should I use tabs or spaces?
Follow your language's convention: 4 spaces for Python, 2 for most JavaScript and YAML, and tabs for Go and Makefiles.
Will conversion change spaces inside my code?
No. Only leading indentation at the start of each line is converted.
Why does Python raise TabError?
The file mixes tabs and spaces for indentation. Convert everything to spaces to fix it.
Can YAML use tabs?
No. YAML forbids tab characters for indentation. Use spaces only.