Spaces to Tabs

Replaces every run of 4 consecutive spaces with a single tab character, useful for converting space-indented code to tab indentation. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-22

Overview

Introduction

Switching a codebase or config file's indentation style from spaces to tabs means precisely converting every indentation level.

This tool converts every 4-space run to a tab.

What Is Spaces to Tabs?

A converter that replaces every run of exactly 4 consecutive space characters with one tab character.

It runs entirely client-side as part of this site's String Tools collection, so nothing you paste is ever uploaded to a server.

How Spaces to Tabs Works

A regex matching 4 consecutive spaces is replaced globally with a tab character.

The transformation happens synchronously in JavaScript the moment you type, with no network round trip involved.

When To Use Spaces to Tabs

Use it when converting space-indented source code or config files to tab indentation.

It's a fast way to get the answer without opening a code editor, a REPL, or writing a one-off script just to check.

Often used alongside Tabs to Spaces and Whitespace Remover.

Features

Advantages

  • Simple, predictable conversion for standard 4-space indentation.
  • Builds its pattern from the width you specify, so the tool is not locked to a single indentation convention.
  • Converts a whole file in one pass, keeping the relative indentation depth of every line intact.

Limitations

  • Only matches exact 4-space runs; 2-space or inconsistent indentation won't convert as expected.
  • Doesn't distinguish leading indentation from spaces elsewhere in a line.

Examples

Converting indented code

Input

    indented
    code

Output

	indented
	code

Each 4-space indent becomes a single tab character.

Best Practices & Notes

Best Practices

  • Verify your source actually uses consistent 4-space indentation before converting, or the result may be uneven.
  • Convert the entire file rather than a fragment, so the indentation stays consistent throughout it.
  • Set your editor's tab width to the space count you converted from, so the file looks unchanged afterwards.

Developer Notes

The replacement regex is built dynamically as `{width} spaces`, defaulting to 4, so it matches runs of exactly that many consecutive spaces rather than any arbitrary run length.

Spaces to Tabs Use Cases

  • Converting a space-indented file to tab indentation
  • Normalizing indentation style across a codebase
  • Preparing code for a style guide that requires tabs

Common Mistakes

  • Running this on 2-space-indented code, which won't match the 4-space pattern.
  • Converting a file that already mixes tabs and spaces, which leaves the mixture in place and can make the indentation worse rather than better.

Tips

  • Use Convert Tabs to Spaces to reverse this operation.
  • Runs anywhere on a line are converted, not only leading indentation, so check aligned comments and tables after running it.

References

Frequently Asked Questions