schankam
schankam

Reputation: 11742

Remove trailing spaces automatically or with a shortcut

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.

Upvotes: 1054

Views: 581425

Answers (11)

Delyan
Delyan

Reputation: 26

I strongly recommend you don't turn on the feature "files.trimTrailingWhitespace": true in VSCode.

If the project you are working on is unnormalized, the feature will trigger many irrelevant modifications in your git commit records, which is a good place for bugs to hide.

Upvotes: 0

me_astr
me_astr

Reputation: 1042

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 of the Settings tab.

Upvotes: 32

Benjamin Pasero
Benjamin Pasero

Reputation: 124174

The shortcut on macOS is now Command-K followed by Command-X.


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

  1. Open Visual Studio Code User Settings (menu FilePreferencesSettingsUser Settings tab).
  2. See this answer below for the GUI setting, or:
  3. Click the enter image description here icon in the top-right part of the window. This will open a document.
  4. 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.
  5. Save the User Settings file.

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

Upvotes: 1747

Dennis H
Dennis H

Reputation: 198

Easiest for me was to open the settings.json file in the .vscode folder in my working folder and insert the above mentioned line:

"files.trimTrailingWhitespace": true

Upvotes: 6

underflow
underflow

Reputation: 2191

As of 2023 (with images)

1) Go to File > Preferences > Settings:

enter image description here


2) In the search bar type file trailing:

enter image description here


3) Check the option

You're all set. Now if you add space(s) at the end of a line (or even consecutive spaces in the middle of a line), once you Ctrl+S, the additional useless spaces will get automatically removed.

Upvotes: 7

Manvendra Bele
Manvendra Bele

Reputation: 23

There is extension in VS Code. This can also be used.

Name: Trailing Spaces Id: shardulm94.trailing-spaces Description: Highlight trailing spaces and delete them in a flash! Version: 0.4.1 Publisher: Shardul Mahadik VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=shardulm94.trailing-spaces

Upvotes: -3

Shivkumar Birnale
Shivkumar Birnale

Reputation: 3336

Menu CodePreferenceSettings

Enter image description here

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

Upvotes: 311

SmoveBB
SmoveBB

Reputation: 120

<Ctr>-<Shift>-<F> 

Format, does it as well.

This removes trailing whitespace and formats/indents your code.

Upvotes: -8

drsnark
drsnark

Reputation: 3043

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.

Upvotes: 7

Ahmad Ehab
Ahmad Ehab

Reputation: 161

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

Visual Studio Code screenshot

Upvotes: 14

ramhiser
ramhiser

Reputation: 3538

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.)

Upvotes: 136

Related Questions