Last updated: Apr 6, 2024
Reading time·3 min
VS Code has a setting that allows you to remove trailing spaces automatically, whenever you save a file.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
Once you enable the setting, every time you save the file, the trailing whitespace gets automatically removed.
You can also use a keyboard shortcut to remove the trailing spaces.
Ctrl + K
Ctrl + X
one after the other.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.
Note: the keyboard shortcut on macOS is: ⌘ + K
⌘ + X
instead.
You can also use the Command Palette to remove the trailing spaces:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.You can also update your settings.json file to automatically remove trailing whitespace.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
"files.trimTrailingWhitespace"
property to true
in the object.{ "files.trimTrailingWhitespace": 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.
On Windows and Linux: press Alt
to show the top menu, then click File
-> Preferences -> Settings.
On macOS: click on Code in the top menu and then Preferences -> Settings.
You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
Once you enable the setting, every time you save the file, the trailing whitespace gets automatically removed.
I've also written an article on how to remove duplicate lines in VS Code.
You can learn more about the related topics by checking out the following tutorials: