Tabs to Spaces Converter
Convert tabs to spaces (or spaces to tabs) instantly. Fix code indentation, set tab width (2, 4, 8), and clean up files for Git/Linting consistency.
About
Inconsistent indentation is the silent killer of clean codebases. Whether you are adhering to a style guide (like PEP8) or fixing mixed content from legacy files, this tool standardizes your whitespace using deterministic logic. It converts the Tab character (ASCII 9) into a fixed sequence of Space characters (ASCII 32), or vice versa.
Unlike simple text replacements, this utility allows you to target Leading Indentation specifically, preserving necessary spacing within strings or comments. It calculates the indentation depth d based on your specified tab width w, ensuring the visual structure of the code remains mathematically identical while changing the underlying byte representation.
Formulas
The conversion logic relies on the relationship between the visual width W and the character count N. For a tab width k:
When converting spaces to tabs (compression), the tool uses integer division to find the number of tabs T and the remainder spaces S:
Reference Data
| Language/Standard | Preferred Indentation | Tab Width (w) | Reasoning |
|---|---|---|---|
| Python (PEP 8) | Spaces | 4 | Strict interpretation required by interpreter. |
| JavaScript (Standard) | Spaces | 2 | Reduces horizontal scroll in deeply nested callbacks. |
| Go (Golang) | Tabs | Variable | Accessibility; allows developers to set own width. |
| HTML/CSS | Spaces | 2 | Compact hierarchy visualization. |
| Linux Kernel | Tabs | 8 | Forces code simplicity (deep nesting looks bad). |
| YAML | Spaces | 2 | Tabs are strictly forbidden by the spec. |