Remove trailing spaces automatically or with a shortcut

后端 未结 7 1298
-上瘾入骨i
-上瘾入骨i 2020-12-07 07:12

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 edit

相关标签:
7条回答
  • 2020-12-07 07:38

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

    0 讨论(0)
  • 2020-12-07 07:38

    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.

    0 讨论(0)
  • 2020-12-07 07:40

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

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

    0 讨论(0)
  • 2020-12-07 07:45
    <Ctr>-<Shift>-<F> 
    

    Format, does it as well.

    This removes trailing whitespace and formats/indents your code.

    0 讨论(0)
  • 2020-12-07 07:47

    Menu FilePreferenceSettings

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

    0 讨论(0)
  • 2020-12-07 07:54

    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.

    0 讨论(0)
提交回复
热议问题