I really do not like trailing spaces. Trailing spaces are any extra spaces or tabs at the end of a line of code or text. They may seem harmless, but they can cause real problems - and they somehow burn my eyes :)

Why trailing spaces matter:

  • File size: Unnecessary whitespace increases file size, slowing down loading and processing.
  • Version control noise: They create false differences in Git diffs, wasting time resolving meaningless conflicts.
  • Bugs: Some languages and tools are sensitive to trailing whitespace, causing unexpected errors.
  • Readability: They make code look messy and inconsistent, reducing clarity and making files harder to maintain.

Quick fix: Manual search and replace

Need to clean up an existing workspace? Use the search-and-replace feature:

  1. Press Ctrl + Shift + H to open the search-and-replace side panel.
  2. Click the .* button to enable regular expression search.
  3. In the search field, type \s+$ (matches one or more whitespace characters at the end of a line).
  4. Leave the replace field empty.
  5. Click Replace All to remove all trailing spaces across your workspace.

Permanent solution: Configure VS Code settings

Rather than manually cleaning up files, configure Visual Studio Code to handle whitespace automatically. You can adjust settings through the Settings UI (Ctrl + ,) or by editing the settings.json file directly.

Trim trailing whitespace on save

Automatically remove trailing whitespace every time you save a file.

Settings UI: Search for Files: Trim Trailing Whitespace and check the box.

JSON:

"files.trimTrailingWhitespace": true,

Render whitespace characters

Make trailing spaces visible in the editor so you can spot them immediately.

Settings UI: Search for Editor: Render Whitespace and select trailing from the dropdown.

JSON:

"editor.renderWhitespace": "trailing",

Other options: none, boundary (except single spaces between words), selection (only on selected text), all.

Show whitespace changes in diffs

For Git users, ensure Visual Studio Code displays whitespace changes in the diff editor for consistency with git diff.

Settings UI: Search for Diff Editor: Ignore Trim Whitespace and uncheck the box.

JSON:

"diffEditor.ignoreTrimWhitespace": false,

Trim final newlines

Remove extra blank lines at the end of files when saving.

Settings UI: Search for Files: Trim Final Newlines and check the box.

JSON:

"files.trimFinalNewlines": true,

The wrap

Removing trailing whitespace in VS Code is a simple yet effective way to improve your coding practice. With these settings in place, your files stay clean automatically - no more manual cleanup, no more noisy diffs, and no more whitespace-related bugs.

Happy coding.

–Jesper