959

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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
schankam
  • 10,778
  • 2
  • 15
  • 26
  • 10
    [ramhiser's answer](https://stackoverflow.com/questions/30884131/remove-trailing-spaces-automatically-or-with-a-shortcut/48932800#48932800) actually covers the shortcut (manual) part: `Ctrl` + `K`, `Ctrl` + `X` – Peter Mortensen Mar 24 '20 at 04:20

10 Answers10

1624

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

qris
  • 7,900
  • 3
  • 44
  • 47
Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • 17
    Yep, I am now using it since it has been released, thank you to the VS code team :) And in addition, here's the shortcut to remove trailing space on the current working file: CTRL+SHIFT+X – schankam Jul 17 '15 at 02:28
  • 29
    Looks like the shortcut on macOS is now Command-K followed by Command-X – schankam Oct 09 '16 at 02:25
  • 6
    Is there a way to have it enabled for all files but disabled for certain file types? Markdown comes to mind, for example. – Prashanth Chandra Jan 16 '17 at 22:25
  • 3
    Currently not, but we are looking into language specific settings for this milestone. See our iteration plan here: https://github.com/Microsoft/vscode/issues/17608 – Benjamin Pasero Jan 17 '17 at 05:52
  • 1
    Is this answer still correct for VS code 1.38.1? I am not seeing the `{}` icon in the User settings tab. – thesamet Sep 14 '19 at 20:40
  • @thesamet It's a document icon now, "Open Settings (JSON)" – simon Sep 19 '19 at 14:01
  • 10
    As of VS Code 1.42, there is a checkbox "Trim Trailing Whitespaces" (as explained by Shivkumar Birnale). No more JSON to edit. – Pierre H. Jan 13 '20 at 22:02
  • [Shivkumar Birnale's answer](https://stackoverflow.com/questions/30884131/remove-trailing-spaces-automatically-or-with-a-shortcut/53663494#53663494) – Peter Mortensen Mar 22 '20 at 18:46
  • 1
    It doesn't seem to be in the command palette in version 1.43.1 (2020-02). – Peter Mortensen Mar 24 '20 at 04:34
  • @rookie1024, for Python I recommend the "Python Indent" extension with the option "Trim Lines With Only Whitespace". It removes the whitespace left behind when creating an blank line. – A. Donda Jul 21 '20 at 04:52
  • 3
    fyi, in the 1.51.1 vs code (MAC), you can search for the `files.trimTrailingWhitespace` in Preference → Settings and toggle the value. – psykid Dec 01 '20 at 07:37
  • Cannot find the *Trim Trailing Whitespace* option in the command pallete.... Was it removed? – Stijn de Witt Sep 26 '22 at 12:32
  • @StijndeWitt It is definitely still present. Checked on the latest version (as of now): Version: 1.79.2. – informatik01 Jul 06 '23 at 07:47
268

Menu CodePreferenceSettings

Enter image description here

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

notapatch
  • 6,569
  • 6
  • 41
  • 45
Shivkumar Birnale
  • 2,906
  • 1
  • 11
  • 17
119

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ramhiser
  • 3,342
  • 3
  • 23
  • 29
  • 3
    On Linux, you can use `Ctrl` + `K` `Ctrl` + `X`. – Penny Liu Apr 19 '19 at 09:47
  • 1
    There doesn't seem to be a match using the command palette in Visual Studio Code 1.43.1 (2020-02). (Same result on both Linux and Windows.) – Peter Mortensen Mar 22 '20 at 18:38
  • 1
    The keyboard shortcut can be discovered by: 1) Open menu `File` →`Preferences` →`Keyboard Shortcuts`. 2) Type "trail" in the search box (at the top) – Peter Mortensen Mar 24 '20 at 04:25
  • On Mac as of today(July 2020) in VSCode 1.46.1 version, you can open settings by pressing ⌘ + , and from there to search trailing whitespace or anything else. – gpap Jul 07 '20 at 13:52
25

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
me_astr
  • 922
  • 1
  • 14
  • 21
12

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

Visual Studio Code screenshot

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmad Ehab
  • 141
  • 1
  • 7
6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
drsnark
  • 2,813
  • 2
  • 15
  • 19
3

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
Dennis H
  • 150
  • 1
  • 8
3

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.

underflow
  • 1,545
  • 1
  • 5
  • 19
-3

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

-8
<Ctr>-<Shift>-<F> 

Format, does it as well.

This removes trailing whitespace and formats/indents your code.

SmoveBB
  • 120
  • 1
  • 6
  • 1
    This requires setting-up a formatter depending on the language of the document. For example, for Python, need to setup [autopep8, yapf, or black](https://code.visualstudio.com/docs/python/editing#_formatting) as the provider. – Gino Mempin Nov 06 '20 at 23:29
  • You didn't mention if you are using an extension or not – Abdulla Dec 27 '20 at 16:43