7

Remove trailing spaces automatically or with a shortcut

 2 years ago
source link: https://stackoverflow.com/questions/30884131/remove-trailing-spaces-automatically-or-with-a-shortcut
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

asked Jun 17, 2015 at 6:46

8 Answers

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

answered Jun 17, 2015 at 6:55

Menu CodePreferenceSettings

Enter image description here

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

answered Dec 7, 2018 at 4:55

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

answered Feb 22, 2018 at 16:42

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.

answered Sep 21, 2018 at 7:44

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

Visual Studio Code screenshot
answered Oct 10, 2019 at 11:53

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.

answered Oct 4, 2017 at 20:27

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
answered Jul 24 at 4:08
<Ctr>-<Shift>-<F> 

Format, does it as well.

This removes trailing whitespace and formats/indents your code.

answered Jun 29, 2020 at 17:27
Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged visual-studio-code vscode-settings or ask your own question.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK