← All tools
// TEXT

Spaces ↔ Tabs online

Convert leading spaces to tabs and tabs to spaces — choose your indent size.

Spaces to Tabs Converter logo
by
CHUNKY
MUNSTER
Tab size (spaces):
INPUT0 CHARS
OUTPUT0 CHARS

About Spaces ↔ Tabs

Converts leading whitespace on each line — useful for switching between space-indented and tab-indented code. The tab size setting controls how many spaces equal one tab when converting. Runs entirely in your browser.

How to Use the Spaces to Tabs Converter

  1. Paste your code or config into the input field.
  2. Pick the tab width (1–8 spaces per tab).
  3. Choose the direction: spaces → tabs or tabs → spaces.
  4. Copy the result back into your editor.

The tabs-vs-spaces argument may never end, but converting between them shouldn't require an editor plugin. This tool expands tabs to N spaces or compresses leading runs of N spaces back to a single tab, where N is the tab width you choose (1–8). Only the leading indentation of each line is touched — strings, comments, and inline alignment are left exactly as you pasted them.

How the Spaces to Tabs Converter Works

Mixed-indentation lines (1 tab + 2 spaces) are handled correctly because each leading whitespace character is mapped column-by-column. That's the same algorithm Python's expandtabs and the standard expand/unexpand utilities use.

Frequently Asked Questions

Will this convert spaces inside string literals?

No — only leading whitespace is touched. A line like print("a b") keeps the four spaces between a and b intact, and only the leading tab (or spaces) is converted.

What tab width should I pick?

It depends on the language convention: Python uses 4, Go uses tabs that render as 4 or 8, Rust uses 4, and many web stacks (HTML/JS) use 2. The default of 4 is the most common compromise.

How are mixed-indentation lines handled?

Each leading whitespace character is expanded or compressed individually based on the chosen tab width. So 1 tab + 2 spaces with a width of 4 expands to 6 spaces, and 6 spaces compresses to 1 tab + 2 spaces.

Why do my tests/diffs break after conversion?

Whitespace-sensitive languages (Python, YAML, Makefile) require consistent indentation throughout a block. Converting one block but not another can break parsing. Always run the converter over the whole file, not a snippet.

Explore the full suite of Text tools and 290+ other free utilities at Chunky Munster.