Convert tabs to spaces — 2, 4 or 8 per tab, with tab stops

Paste code or text with tab characters and every tab becomes spaces, either a fixed number per tab or exactly enough to reach the next tab stop.

Live demo · real result from this tool
Spaces per tab

Result

function hello() {    return    "hi";}

Processed on your device — nothing is uploaded.

How to use Tab to Spaces

  1. Paste your text or code into Your text. The sample is a small function indented with tabs.
  2. Pick Spaces per tab: 2, 4 or 8. The default is 4.
  3. Tick Align to tab stops (like an editor) if tabs appear in the middle of lines and you want columns to stay aligned.
  4. Copy the result. It is shown in a monospaced font so you can check the alignment.

What it does and when to use it

A tab is one character, but different programs show it at different widths. Code indented with tabs can look neat in one editor and staggered in another, in an email or on a web page. Converting tabs to spaces fixes the look everywhere.

Common reasons to convert:

  • a style guide or linter requires spaces;
  • you are pasting code into a forum, chat, document or slide where tabs show too wide;
  • YAML files, which do not allow tabs for indentation;
  • tab-separated data that you want to read as aligned plain-text columns.

Fixed replacement (the default) is right for indentation at the start of lines, because there every tab should become the same amount of space. Tab stops are right for tabs after text. A word of 2 letters and a word of 9 letters followed by a tab both reach the same column, just as in a code editor.

After converting, Trim Lines can remove trailing spaces, and Text Padder can widen columns further. To squeeze runs of spaces inside lines, use Remove Extra Spaces.

How it works

In fixed mode, every tab character in the text is replaced with the chosen number of spaces. That is all.

In tab-stop mode, the tool walks along each line keeping track of the column. Ordinary characters move it forward by 1. At a tab, it inserts size − (column mod size) spaces, which lands exactly on the next multiple of the size. The column restarts at 0 on each new line. It all happens in your browser.

Worked examples

Two-space indent. A three-line block with return 1; indented by one tab, at 2 spaces per tab, comes out with return 1; indented by two spaces.

Columns with tab stops. “Name”, “Jo” and “Alexandra”, each followed by a tab and a value, at size 8 with tab stops. “Name” (4 letters) gets 4 spaces and “Jo” gets 6, so “Age” and “31” both start at column 8. “Alexandra” (9 letters) passes the first stop, so it gets 7 spaces to reach column 16.

Same columns without tab stops. Each tab becomes 8 spaces regardless of position, giving Name Age with 8 spaces, and the values no longer line up.

Limits and tips

  • Tab stops count each visible character as one column. Wide characters such as emoji or Chinese characters can still make columns look uneven.

Frequently asked questions

Should I use 2, 4 or 8 spaces?
Follow the style of the project or language. Python's PEP 8 style guide asks for 4 spaces per indent level. Many JavaScript, Ruby and YAML projects use 2. 8 is the traditional width of a tab in terminals and Unix tools.
What does "Align to tab stops" change?
Without it, every tab becomes the same number of spaces wherever it is. With it, a tab moves the text to the next column that is a multiple of the tab size, which is how editors and terminals show tabs. Use it for tabs in the middle of a line, such as columns of data.
Does it touch spaces that are already there?
No. Only tab characters are replaced. Existing spaces stay as they are, so mixed indentation keeps its spaces and loses only its tabs.
How do I go back from spaces to tabs?
Use Spaces to Tab with the same size. It converts runs of spaces at the start of each line back into tabs.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close