Indentation converter — tabs to spaces, spaces to tabs, 4 to 2 spaces

Paste code in any language and switch its indentation between tabs and spaces, or change the indent size, for example from 4 spaces to 2, while the rest of each line stays as it is.

Live demo · real result from this tool
Convert to

Converted

function f() {  if (x) {    return 1;  }}

Processed on your device — nothing is uploaded.

How to use the indentation converter

  1. Paste your code into the Code box. The sample is a small function indented with tabs.
  2. Choose Convert to: Spaces or Tabs.
  3. Set Tab width (spaces per level in the input), 1–12, default 4.
  4. For spaces, set Spaces per level in the output (default 2).
  5. Leave Only change leading indentation ticked, then copy the Converted text.

What it does and when to use it

Teams and languages disagree about indentation. Go and Makefiles use tabs, Python’s PEP 8 asks for 4 spaces, much JavaScript and YAML uses 2. Code copied between projects, or from a Stack Overflow answer, rarely matches the house style, and a mixed file makes diffs noisy. This converter changes only the indentation, so you can match a project’s editor settings before committing.

It does not look at brackets or keywords, so it works on any text, from HTML and SQL to Markdown lists. If the indentation itself is wrong rather than just the wrong size, use a structure-aware tool such as the code beautifier. To fix line endings and trailing spaces at the same time, follow up with the whitespace normalizer, and use the code diff viewer to confirm nothing else changed.

How it works

For each line, in your browser, the tool:

  1. Measures the leading whitespace in columns. A space adds 1; a tab jumps to the next multiple of the tab width.
  2. Splits that width into whole levels and a remainder.
  3. Writes the new indent: one tab per level for Tabs, or levels × your output size for Spaces, followed by the remainder as spaces.
  4. Copies the rest of the line unchanged, unless Only change leading indentation is unticked. Then each tab inside the line becomes tab-width spaces (for Spaces) or stays a tab.

Worked examples

These are the tool’s own outputs.

Tabs to 2 spaces (the sample).

function f() {
  if (x) {
    return 1;
  }
}

Each tab was one level; each level is now 2 spaces.

HTML from 4 spaces to tabs. <ul> with two <li> lines indented by 4 spaces, converted to Tabs, gives one tab before each <li>.

Python from 4 spaces to 2. def f():, if x: and return 1 at 0, 4 and 8 spaces come out at 0, 2 and 4.

A partial level. With Tabs and a width of 4, two (2 spaces) stays as 2 spaces, and a 6-space line becomes a tab plus 2 spaces, so alignment is not lost.

Limits and tips

  • Inner tabs are not aligned to tab stops. With the leading-only box unticked, a, tab, b becomes a and 4 spaces then b, not a padded to column 4.
  • Set the input width correctly. If a 2-space file is read with width 4 and output size 2, lines at 2 and 4 spaces both end up at 2, so two levels merge.
  • Makefiles need tabs before commands; never convert them to spaces.

Frequently asked questions

What does Tab width mean here?
It is how many columns one indent level takes in your input. A tab counts as one full level, and that many spaces also count as one level. Set it to the step size your input really uses, often 4, or 2 for web code.
How do I change 4-space indentation to 2 spaces?
Keep Convert to on Spaces, set Tab width to 4 and Spaces per level in the output to 2. Each level of 4 spaces becomes 2.
Should I untick Only change leading indentation?
Usually not. With it unticked, tabs in the middle of lines are also replaced by a fixed number of spaces, which can break tab-aligned columns and change tabs inside strings. It is useful for plain text where tabs only separate words.
Is this safe for Python or YAML?
Yes, as long as the input is indented consistently, because every line moves by whole levels. For Python with mixed or uneven indents, the Python code cleaner checks the block structure instead.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close