837

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
  • 28,518
  • 9
  • 66
  • 91
schankam
  • 9,547
  • 2
  • 14
  • 25
  • 8
    [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

7 Answers7

1449

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

Mark
  • 868
  • 16
  • 27
Benjamin Pasero
  • 100,816
  • 12
  • 71
  • 52
  • 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
  • 22
    Looks like the shortcut on macOS is now Command-K followed by Command-X – schankam Oct 09 '16 at 02:25
  • 5
    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
  • 7
    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
  • 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
  • 1
    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
208

Menu CodePreferenceSettings

Enter image description here

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

notapatch
  • 5,708
  • 5
  • 36
  • 40
Shivkumar Birnale
  • 2,306
  • 1
  • 10
  • 17
98

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,030
  • 21
  • 100
  • 124
ramhiser
  • 3,072
  • 1
  • 22
  • 29
  • 3
    On Linux, you can use `Ctrl` + `K` `Ctrl` + `X`. – Penny Liu Apr 19 '19 at 09:47
  • 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
21

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,030
  • 21
  • 100
  • 124
me_astr
  • 862
  • 1
  • 12
  • 21
11

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

Visual Studio Code screenshot

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Ahmad Ehab
  • 131
  • 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,030
  • 21
  • 100
  • 124
drsnark
  • 2,443
  • 2
  • 14
  • 18
-6
<Ctr>-<Shift>-<F> 

Format, does it as well.

This removes trailing whitespace and formats/indents your code.

SmoveBB
  • 114
  • 1
  • 6
  • 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