Spaces to tabs converter

A quick way for coders and writers to standardise indentation: each group of spaces becomes one tab at the width you choose.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter text, tab width and what to convert.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

Leading mode: find the column where the indentation ends (a tab jumps to the next multiple of the width), then write floor(column / width) tabs plus the remainder as spaces. All mode: every N consecutive spaces become one tab.

Example

Inputs: Tab width = 4 spaces, What to convert = Leading indentation only. Result: Result: function greet() { if (ok) { return 'hi'; } }; Tabs in the result: 4.

function greet() {
    if (ok) {
        return 'hi';
    }
}
function greet() {
	if (ok) {
		return 'hi';
	}
}

Limits and caveats

  • All mode also changes spaces inside strings and comments.
  • Width is counted in characters, so wide characters such as emoji count as one column.

Questions about this tool

What happens to spaces that do not fill a whole tab?

They stay as spaces after the tabs. For example, 6 spaces at width 4 become one tab and two spaces.

Does it handle lines that mix tabs and spaces?

Yes. In leading mode the mixed indentation is measured as one column, then rebuilt with tabs.

What tab width is common?

4 spaces is the most common, and many JavaScript and YAML projects use 2.