Remove trailing spaces automatically or with a shortcut

Visual Studio-CodeVscode Settings

Visual Studio-Code Problem Overview


Is there a way to remove trailing spaces in Visual Studio Code (automatically or with a shortcut)?

I've been looking for this in the command palette and in the editor settings, but I cannot find what I am looking for.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

You can enable whitespace trimming at file save time from settings:

  1. Open Visual Studio Code User Settings (menu FilePreferencesSettingsUser Settings tab).
  2. Click the enter image description here icon in the top-right part of the window. This will open a document.
  3. Add a new "files.trimTrailingWhitespace": true setting to the User Settings document if it's not already there. This is so you aren't editing the Default Setting directly, but instead adding to it.
  4. Save the User Settings file.

We also added a new command to trigger this manually (Trim Trailing Whitespace from the command palette).

Solution 2 - Visual Studio-Code

Menu CodePreferenceSettings

Enter image description here

Check the "Trim Trailing Whitespace" option - "When enabled, will trim trailing whitespace when saving a file".

Solution 3 - Visual Studio-Code

Not only can you change the Visual Studio Code settings to trim trailing whitespace automatically, but you can also do this from the command palette (Ctrl+Shift+P):

Command Palette: Trim Trailing Whitespace

You can also use the keyboard shortcut:

  • Windows, Linux: Ctrl+K, Ctrl+X
  • Mac: + k, + x.

(I'm using Visual Studio Code 1.20.1.)

Solution 4 - Visual Studio-Code

In recent Visual Studio Code versions you can find settings here:

Menu FilePreferenceSettingsText EditorFiles → (scroll down a bit) Trim Trailing Whitespace

This is for trimming whitespace when saving a file.

Or you can search "Trim Trailing Whitespace" in the top search bar.

Solution 5 - Visual Studio-Code

Visual Studio Code, menu FilePreferenceSettings → search for "trim":

Visual Studio Code screenshot

Solution 6 - Visual Studio-Code

Have a look at the EditorConfig plugin.

By using the plugin you can have settings specific for various projects. Visual Studio Code also has IntelliSense built-in for .editorconfig files.

Solution 7 - Visual Studio-Code

<Ctr>-<Shift>-<F> 

Format, does it as well.

This removes trailing whitespace and formats/indents your code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionschankamView Question on Stackoverflow
Solution 1 - Visual Studio-CodeBenjamin PaseroView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeShivkumar BirnaleView Answer on Stackoverflow
Solution 3 - Visual Studio-CoderamhiserView Answer on Stackoverflow
Solution 4 - Visual Studio-Codeme_astrView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeAhmad EhabView Answer on Stackoverflow
Solution 6 - Visual Studio-CodedrsnarkView Answer on Stackoverflow
Solution 7 - Visual Studio-CodeSmoveBBView Answer on Stackoverflow