ChatGPT解决这个技术问题 Extra ChatGPT

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.

ramhiser's answer actually covers the shortcut (manual) part: Ctrl + K, Ctrl + X

M
Mark

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

Open Visual Studio Code User Settings (menu File → Preferences → Settings → User Settings tab). Click the icon in the top-right part of the window. This will open a document. 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. Save the User Settings file.

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


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
Looks like the shortcut on macOS is now Command-K followed by Command-X
Is there a way to have it enabled for all files but disabled for certain file types? Markdown comes to mind, for example.
Currently not, but we are looking into language specific settings for this milestone. See our iteration plan here: github.com/Microsoft/vscode/issues/17608
As of VS Code 1.42, there is a checkbox "Trim Trailing Whitespaces" (as explained by Shivkumar Birnale). No more JSON to edit.
n
notapatch

Menu Code → Preference → Settings →

https://i.stack.imgur.com/hZrUY.png

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


interface changed
In Codium, it starts with a click on "File" in the menu.
Can I exclude some types of files from getting their trailing whitespaces trimmed?
P
Peter Mortensen

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

https://i.stack.imgur.com/wmfl7.png

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


On Linux, you can use Ctrl + K Ctrl + X.
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.)
The keyboard shortcut can be discovered by: 1) Open menu FilePreferencesKeyboard Shortcuts. 2) Type "trail" in the search box (at the top)
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.
P
Peter Mortensen

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

Menu File → Preference → Settings → Text Editor → Files → (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.


Had this selected - didn't do what it said (as revealed in git). Hope adding it manually to the user-settings file works.
Alternative: Type "trim" in the search box after the "Settings" window has been opened (at the top)
P
Peter Mortensen

Visual Studio Code, menu File → Preference → Settings → search for "trim":

https://i.stack.imgur.com/pVxe1.png


P
Peter Mortensen

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.


S
SmoveBB
<Ctr>-<Shift>-<F> 

Format, does it as well.

This removes trailing whitespace and formats/indents your code.


This requires setting-up a formatter depending on the language of the document. For example, for Python, need to setup autopep8, yapf, or black as the provider.
You didn't mention if you are using an extension or not