Spaces to Tabs Converter

Paste your text and set the tab width to convert leading spaces into tabs.

Result

if ready: start() loop() stop()
4
4

How it is calculated

The tool looks at each line separately and only at the spaces at its very start. With tab width w it counts the leading spaces n, then writes tabs = ⌊n ÷ w⌋ tab characters followed by n mod w spaces that are left over, and then the rest of the line unchanged. Spaces inside a line, after the first non-space character, are never touched. A line that already begins with a tab stops the count at that tab, so spaces after it stay as spaces. The width is rounded to a whole number and must be at least 1. The result reports how many tab characters were inserted and how many lines were read, where a trailing line break counts as one more (empty) line. Line endings in the output are always plain newlines.

Worked example

Take four lines with a tab width of 4: if ready: with no indent, start() indented by 4 spaces, loop() by 8 and stop() by 6. The first line has 0 tabs. The second has 4 ÷ 4 = 1 tab. The third has 8 ÷ 4 = 2 tabs. The fourth has 6 ÷ 4 = 1 tab with 2 spaces left over, so it begins with a tab and two spaces. In total 4 tabs were inserted across 4 lines.

Try this example →

Common mistakes

  • Choosing a width that does not match the file. If the code was indented with 2 spaces and you convert at 4, a 2-space line gets no tab at all and a 6-space line gets one tab plus 2 spaces.
  • Expecting the leftover spaces to disappear. Indentation that is not a multiple of the width keeps the extra spaces after the tabs, which can look misaligned; fix the odd lines first.
  • Expecting spaces in the middle of a line to change. Alignment spaces between words or columns are deliberately left alone, only the start of each line is converted.
  • Running it on a file that already mixes tabs and spaces. Spaces that come after a tab at the start of a line are not converted, so such lines stay mixed.
  • Using it on formats that need spaces, such as YAML, or on Markdown where a code block counts indentation. Tabs may break them.

About this calculator

Source code and configuration files sometimes mix indentation styles — leading spaces in one section, tab characters in another — which can cause misaligned display, diff noise, or errors in languages like Python where indentation is syntactically meaningful. Converting leading spaces to tabs at a consistent width is a standard cleanup step to make indentation uniform again.

This tool scans each line, measures the run of leading spaces, and replaces every block matching your chosen tab width with an actual tab character, line by line. It's a routine utility for developers standardizing a codebase's formatting, migrating a project between editors or teams with different indentation conventions, or preparing a file for a linter or formatter that expects tabs rather than spaces.

Was this helpful?

You can pick up to 3 reactions.

Comments

  • Be the first to comment.

Popular calculators

All Calculators