Bulk Replace
Regex

How to Remove Trailing Spaces and Tabs From Every File in a Project

Strip trailing spaces and tabs from every line of every file in a folder. The regex to use, editor settings that prevent it, and what to check first.

Updated 5 minute read The Bulk Replace team

To remove trailing whitespace from all files in a folder, run a regex replace with multiline mode on: find [ \t]+(?=\r?$) and replace it with nothing. That strips spaces and tabs from the end of every line, in Windows and Unix files alike. Run it across the folder with an editor's replace in files, a short PowerShell loop, or a batch tool with a preview. Then turn on your editor's trim-on-save setting or add an .editorconfig rule so the spaces do not come back.

Trailing whitespace is invisible, but it creates noisy diffs, fails lint checks and occasionally breaks things. Here is how to clean a whole project safely.

The pattern, and why it is not just [ \t]+$

The obvious pattern is [ \t]+$: one or more spaces or tabs at the end of a line. It works on files with Unix line endings, and in most editors. It can fail on Windows files in .NET-based tools such as PowerShell.

Windows text files end each line with two characters, \r\n. In .NET regex with multiline mode, $ matches just before the \n, which is after the \r. A line like value  \r\n therefore has the \r sitting between the spaces and the position $ wants, so [ \t]+$ finds nothing.

The fix is [ \t]+(?=\r?$). The lookahead allows an optional \r before the end of the line without including it in the match, so only the spaces and tabs are removed and the line ending stays intact. It behaves the same in .NET, JavaScript and Boost, so you can use one pattern everywhere.

PatternUnix filesWindows files in .NETNotes
[ \t]+$WorksMisses lines ending in \r\nFine in most editors
[ \t]+(?=\r?$)WorksWorksNeeds multiline mode
[ \t]+(?=\r?\n|$)WorksWorksNo multiline mode needed
\s+$WorksWorksAvoid: also eats blank lines and line breaks

The last row is a common trap. \s includes line breaks, so \s+$ can swallow whole empty lines and join paragraphs. Stick to [ \t].

Before you run it: files to exclude

In most code and config files, trailing whitespace has no meaning. A few formats are exceptions:

  • Markdown (.md). Two or more trailing spaces create a hard line break. Removing them joins lines that the author split on purpose. Exclude Markdown, or review those matches.
  • Fixed-width data files. Some exports pad each field with spaces to a set width. Removing padding from the last column changes the record length.
  • Test fixtures and snapshots. Expected output files may include trailing spaces deliberately.
  • Diff and patch files (.diff, .patch). Context lines must match the original exactly.
  • Strings that span lines. Spaces at the end of a line inside a multi-line string or a <pre> block are content.

Also skip folders you do not own, such as node_modules, vendor and build output. Our guide to limiting a replace to specific file types and folders shows the filter syntax for each tool.

Option 1: VS Code

For one open file, run Trim Trailing Whitespace from the Command Palette (Ctrl+Shift+P), or press Ctrl+K Ctrl+X. For a whole folder:

  1. Open the folder and press Ctrl+Shift+H.
  2. Turn on regex (Alt+R) and search for [ \t]+$. VS Code handles Windows line endings itself, so the simple pattern works here.
  3. Leave the replace box empty and set files to include, for example *.js, *.css, *.html.
  4. Check the results count, then click Replace All.

Option 2: Notepad++

For an open file, use Edit → Blank Operations → Trim Trailing Space. For a folder, open Find in Files (Ctrl+Shift+F), search for [ \t]+$ in Regular expression mode with an empty replacement, set the filters and directory, and click Replace in Files. As always with Replace in Files, files that are not open are written to disk with no undo, so make a copy first.

Option 3: PowerShell

This loop reads each file as one string, strips the trailing spaces and writes only the files that changed:

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

  • (?m) turns on multiline mode so $ matches at each line end.
  • -Raw keeps the original line endings.
  • -Encoding UTF8 in Windows PowerShell 5.1 writes UTF-8 with a byte order mark. If your files are UTF-8 without one, use PowerShell 7, where -Encoding utf8NoBOM is available, or the file will gain three invisible bytes at the start.
  • The last $_.FullName prints each changed file, which gives you a simple log.

For more on these details, see replacing text in multiple files with PowerShell.

Option 4: a batch tool

In Bulk Replace, add a regex rule with [ \t]+(?=\r?$) and an empty replacement, filter to your source extensions, and preview. The before/after diff makes invisible changes visible, which is handy here: you can see that a Markdown file is about to lose its hard line breaks and untick it. Bulk Replace preserves each file's encoding, so a UTF-8 file without a byte order mark stays that way, and every run is backed up for one-click undo.

It pairs well with a second rule to remove or collapse blank lines, run in the same pass.

Stop trailing spaces coming back

A one-off clean-up only lasts until the next edit. Set your tools to trim on save:

ToolSetting
VS Code"files.trimTrailingWhitespace": true in settings.json, or tick Files: Trim Trailing Whitespace in Settings
EditorConfig (works across many editors)trim_trailing_whitespace = true in .editorconfig
Markdown exception in EditorConfigA [*.md] section with trim_trailing_whitespace = false
Gitgit diff --check reports trailing whitespace in changes before you commit

A minimal .editorconfig at the root of the project has five lines:

  1. root = true
  2. [*]
  3. trim_trailing_whitespace = true
  4. [*.md]
  5. trim_trailing_whitespace = false

Each numbered item is one line of the file. The [*] section applies to every file, and the later [*.md] section overrides it for Markdown.

VS Code needs the EditorConfig extension to read this file. Many other editors read it natively.

Commit it separately

A whitespace clean-up touches many lines in many files. If you are using Git, commit it on its own with a clear message, before or after your real work, never mixed in with it. That keeps code reviews readable, and anyone looking at history later can skip the whitespace commit. Reviewers can also hide whitespace-only changes with git diff -w.

Quick reference

  • Any tool, multiline on: [ \t]+(?=\r?$) → nothing
  • Editors (VS Code, Notepad++): [ \t]+$ → nothing
  • Never use \s+$: it removes line breaks too
  • Exclude Markdown, fixtures, patches and fixed-width data

Frequently asked questions

What regex finds trailing whitespace?

[ \t]+$ with multiline mode on matches spaces and tabs at the end of each line. In .NET, $ matches before \n only, so use [ \t]+(?=\r?$) for Windows line endings.

Is it safe to remove trailing whitespace everywhere?

Mostly, but Markdown uses two trailing spaces as a line break, and some data formats treat spaces as content. Exclude those files.

How do I stop trailing spaces coming back?

Turn on files.trimTrailingWhitespace in VS Code, or add trim_trailing_whitespace = true to an .editorconfig file.

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