Remove trailing spaces automatically in Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
3 min

banner

# Remove trailing spaces automatically in Visual Studio Code

VS Code has a setting that allows you to remove trailing spaces automatically, whenever you save a file.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Search for files trim and tick the checkbox next to Files: Trim Trailing Whitespace.

check trim trailing whitespace checkbox

Once you enable the setting, every time you save the file, the trailing whitespace gets automatically removed.

trailing whitespace automatically removed

# Remove trailing spaces using a keyboard shortcut

You can also use a keyboard shortcut to remove the trailing spaces.

  1. On Windows and Linux: press Ctrl + K Ctrl + X one after the other.
  2. On macOS: press Cmd + K Cmd + X one after the other.

The keyboard shortcut is shown in this section of the docs you can also hover over the keys in the Key column to see the macOS equivalent.

using keyboard shortcut to remove trailing whitespace

Note: the keyboard shortcut on macOS is: ⌘ + K ⌘ + X instead.

# Remove trailing spaces using the Command Palette

You can also use the Command Palette to remove the trailing spaces:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type trim and select Trim Trailing Whitespace.

trim trailing whitespace command palette

# Automatically remove trailing whitespace by updating settings.json

You can also update your settings.json file to automatically remove trailing whitespace.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Set the "files.trimTrailingWhitespace" property to true in the object.
settings.json
{ "files.trimTrailingWhitespace": true, }

set files trim trailing whitespace to true

Once you set the files.trimTrailingWhitespace setting to true, trailing whitespace is removed every time you save a file.

You can also access the settings menu via the top menu.

  1. On Windows and Linux: press Alt to show the top menu, then click File -> Preferences -> Settings.

  2. On macOS: click on Code in the top menu and then Preferences -> Settings.

click file preferences settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Search for files trim and tick the checkbox next to Files: Trim Trailing Whitespace.

check trim trailing whitespace checkbox

Once you enable the setting, every time you save the file, the trailing whitespace gets automatically removed.

trailing whitespace automatically removed

I've also written an article on how to remove duplicate lines in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.