Home Text Tools Space to Tab Converter
Text

Space to Tab Converter

Convert leading spaces to tab characters for code that requires tab indentation. Set your space width (2, 4, or 8), convert only leading indentation or all spaces, and see a live whitespace visualiser showing every tab and space.

🔒 Browser-based↔ Bidirectional👁 Whitespace visualiser⚡ Instant
Switch Tool:
🔒 100% Private — All processing runs in your browser. Your text is never sent to any server.

📖How to Use the Space to Tab Converter

  1. 1
    Paste your space-indented text

    Paste code or text that uses spaces for indentation. The tool works with any indentation depth — 2-space, 4-space, or 8-space styles. Mixed indentation is handled gracefully.

  2. 2
    Set your tab width

    Choose how many spaces equal one tab: 2, 4, or 8 (matching your project standard). The tool groups consecutive leading spaces by your tab width and replaces each group with a single tab character. Remainder spaces are left as spaces.

  3. 3
    View, copy or download

    The output appears instantly. Enable the whitespace visualiser to see tabs as → and spaces as · to verify the conversion is correct. Copy to clipboard or download the converted file.

💡Quick Reference

LanguageConvention
Python (PEP 8)4 spaces
JavaScript / TS2 spaces
Go (gofmt)Tabs (required)
PHP / Java4 spaces
MakefileTabs (required)

Frequently Asked Questions

Why would I want to convert spaces to tabs?

Some languages and projects require tab-based indentation. Go (Golang) uses tabs as the official style enforced by gofmt. Makefiles require tab characters (not spaces) before recipe lines — a space will cause "missing separator" errors. Converting a space-indented file to tabs can also reduce file size since one tab character replaces up to 8 space characters.

Does the tool handle partial indentation correctly?

Yes. If your tab width is 4 and a line starts with 9 spaces, the tool produces 2 tabs and 1 space — 2 complete tab-widths (8 spaces) are converted to 2 tabs, and the remaining 1 space stays as a space. This prevents misalignment in deeply nested code.

What is the difference between leading spaces only and all spaces?

Leading spaces only converts only the spaces at the very start of each line — the indentation. Spaces within the line content are left untouched. This is the safe mode for code. All spaces converts every space character in the entire text to a tab, useful for converting TSV-style space-delimited data.

Will this break Python code?

Python 3 disallows mixing tabs and spaces for indentation. If you convert all leading spaces to tabs consistently with the correct tab width, the code remains valid. Always test Python files after conversion and consider using Black for production Python indentation.

How does this compare to using sed or awk?

The shell command unexpand -t 4 converts leading spaces to tabs. This tool does the same without requiring a terminal, handles Windows CRLF line endings correctly, and provides visual confirmation via the whitespace visualiser — ideal for quick one-off conversions.

What line endings does the tool preserve?

The tool preserves your line endings. Files with Windows-style CRLF line endings maintain CRLF; Unix LF files maintain LF. You can also choose to normalise line endings to LF using the line ending option.