Bulk Replace
How-to

How to Convert Tabs to Spaces (or Spaces to Tabs) in Many Files

Convert tab indentation to spaces, or spaces to tabs, across many files at once. Regex that only touches indentation, plus editor and .editorconfig tips.

Updated 5 minute read The Bulk Replace team

To replace tabs with spaces in multiple files, the quick method is a regex find and replace across the folder: find \t and replace it with four spaces (or two, to match your project). That changes every tab, including tabs in the middle of lines. To convert only the indentation at the start of each line, find (?<=^\t*)\t with multiline mode on, which works in .NET and JavaScript-based tools. For the reverse, spaces to tabs, find (?<=^(?: {4})*) {4} and replace with \t. Exclude Makefiles, which must keep their tabs.

This guide explains when to convert every tab and when to touch only indentation, how to do it in editors and PowerShell, and how to stop mixed indentation coming back.

Every tab, or only indentation?

Tabs appear in two places, and they mean different things:

  • Leading tabs are indentation. Converting them to spaces is the usual goal and is safe in nearly every language.
  • Tabs inside a line may be alignment between columns, part of a string literal, or a separator in data such as tab-separated values. Converting those changes meaning or breaks the layout.
GoalFindReplaceEngines
Every tab\t4 spacesAll
Leading tabs only(?<=^\t*)\t4 spaces.NET, JavaScript (multiline on)
Leading spaces to tabs (4 per tab)(?<=^(?: {4})*) {4}\t.NET, JavaScript (multiline on)
Leading spaces to tabs (2 per tab)(?<=^(?: {2})*) {2}\t.NET, JavaScript (multiline on)

The leading-only patterns use a variable-length lookbehind. (?<=^\t*)\t reads as "a tab that has only tabs between it and the start of the line", so a tab after any other character is skipped. The spaces-to-tabs version converts each complete group of four leading spaces. A line indented with six spaces becomes one tab plus two spaces, which is usually what you want to review by hand anyway.

Notepad++ and other Boost or PCRE tools do not allow variable-length lookbehind. In those, use the editor's own commands, described below. Lookbehind is covered in more depth in lookahead and lookbehind examples.

In the replacement, \t means a tab in VS Code and Notepad++. In .NET and PowerShell the replacement is not parsed for backslash escapes, so pass a real tab character instead, for example "`t" in PowerShell.

A note on tab stops

A tab is not "four spaces". It moves the cursor to the next tab stop. A tab after three characters of text only advances one column if tab stops are every four columns. That means a blind \t to four spaces replacement inside a line can break alignment of comments or table-like text. Leading tabs do not have this problem, because they always start at a tab stop. That is one more reason to convert indentation only, unless you know mid-line tabs are rare.

Files to leave alone

FileWhy
Makefile, *.mkMake requires recipe lines to start with a tab. Spaces cause a "missing separator" error.
*.tsv and tab-delimited exportsTabs are the column separator.
Go source (*.go)The standard formatter, gofmt, indents with tabs and will put them back.
Heredocs with <<- in shell scriptsThat form strips leading tabs only, not spaces.
Minified files and third-party codeNot yours to restyle; a vendor update would undo it anyway.

Python deserves a mention too. Python 3 refuses files that mix tabs and spaces inconsistently in the same block, so convert a whole file at once, never half of it. Converting all leading tabs to four spaces is safe and matches the usual Python style.

Method 1: VS Code

For one file, open the Command Palette (Ctrl+Shift+P) and run Convert Indentation to Spaces or Convert Indentation to Tabs. The status bar shows the current setting (for example "Spaces: 4" or "Tab Size: 4"); click it to change the size first.

For a folder, use Search (Ctrl+Shift+H) with regex on:

  1. Search for (?<=^\t*)\t.
  2. Type four spaces in the replace box.
  3. Set files to include, for example *.js, *.css, and put Makefile, *.mk, *.tsv in files to exclude.
  4. Check the results, then Replace All.

The VS Code replace in all files guide covers the include and exclude boxes in detail.

Method 2: Notepad++

Notepad++ has built-in commands under Edit → Blank Operations: TAB to Space, Space to TAB (All) and Space to TAB (Leading). They respect tab stops and work on the current file. The tab width comes from Settings → Preferences → Language (Tab Settings).

For many files, open them all and run the command in each tab, or use Find in Files with \t and four spaces if converting every tab is acceptable. Notepad++ does not accept the variable-length lookbehind patterns above.

Method 3: PowerShell

This converts leading tabs to four spaces in every .js and .css file under the current folder, and lists the files it changed:

Get-ChildItem -Recurse -File -Include *.js,*.css | ForEach-Object { $t = Get-Content $_.FullName -Raw; $n = $t -replace '(?m)(?<=^\t*)\t', ' '; if ($n -ne $t) { Set-Content $_.FullName $n -NoNewline; $_.FullName } }

For spaces to tabs, the replacement needs a real tab character, so use double quotes with the backtick escape:

$n = $t -replace '(?m)(?<=^(?: {4})*) {4}', "`t"

As with any PowerShell rewrite, add -Encoding to Set-Content in Windows PowerShell 5.1 if your files contain non-English characters, and test on a copy first.

Method 4: a batch tool with preview

Bulk Replace handles this as one regex rule run over a folder tree, with extension filters to include your source files and leave out Makefiles and data files. Whitespace changes are hard to see in a normal diff, but the preview still gives you the match count per file, which is the quickest way to spot a file that should not be there, such as a TSV export with thousands of matches. Bulk Replace backs up every file before writing, so if a build fails afterwards, one click restores the originals.

Keep it consistent afterwards

Once converted, tell your tools which style to use so new code matches. An .editorconfig file at the project root works across many editors:

  • [*] with indent_style = space and indent_size = 4
  • [Makefile] with indent_style = tab
  • [*.go] with indent_style = tab

In VS Code, also check Editor: Insert Spaces and Editor: Detect Indentation. Detect Indentation follows each file's existing style, which is helpful until the file is mixed, then it can guess wrong.

If the project is under Git, commit the conversion on its own. Reviewers can use git diff -w to hide whitespace changes. While you are tidying, you may also want to remove trailing whitespace in the same pass.

Quick reference

  • Every tab: \t → spaces
  • Leading tabs: (?<=^\t*)\t → spaces (multiline, .NET or JavaScript)
  • Leading spaces to tabs: (?<=^(?: {4})*) {4} → tab
  • Exclude Makefiles, TSV files and Go source

Frequently asked questions

How do I match a tab character?

Use \t in a regex search. In plain text mode many search boxes will not accept a typed tab, so switch to regex.

How do I replace only leading tabs?

Replacing every \t also changes tabs inside lines and strings. Use (?<=^\t*)\t with multiline mode in .NET, or an editor's convert-indentation command, to touch only the indentation.

Will converting tabs break Makefiles?

Yes. Make requires recipe lines to start with a tab, so exclude Makefile and *.mk files from the run.

Get three guides like this every month, plus software update notices.Subscribe →