Show whitespace characters in Visual Studio Code

后端 未结 13 1558
不知归路
不知归路 2020-12-04 05:12

Is it possible to show whitespace characters, like the space character, in Visual Studio Code?

There doesn\'t appear to be an option for it in the settings.js

相关标签:
13条回答
  • 2020-12-04 05:23

    I'd like to offer this suggestion as a side note.
    If you're looking to fix all the 'trailing whitespaces' warnings your linter throws at you.
    You can have VSCode automatically trim whitespaces from an entire file using the keyboard chord.
    CTRL+K / X (by default)

    I was looking into showing whitespaces because my linter kept bugging me with whitespace warnings. So that's why I'm here.

    0 讨论(0)
  • 2020-12-04 05:24

    Hit the F1 button, then type "Toggle Render Whitespace" or the parts of it you can remember :)

    I use vscode version 1.22.2 so this could be a feature that did not exist back in 2015.

    0 讨论(0)
  • 2020-12-04 05:25

    *** Update August 2020 Release *** see https://github.com/microsoft/vscode/pull/104310

    "editor.renderWhitespace": "trailing" // option being added

    Add a new option ('trailing') to editor.renderWhitespace that renders only 
    trailing whitespace (including lines with only whitespace).
    

    *** Update February 2020 Release *** see https://github.com/microsoft/vscode/issues/90386

    In v1.43 the default value will be changed to selection from none as it was in v1.42.

    "editor.renderWhitespace": "selection"  // default in v1.43
    

    Update for v1.37: adding the option to render whitespace within selected text only. See v1.37 release notes, render whitespace.

    The editor.renderWhitespace setting now supports a selection option. With this option set, whitespace will be shown only on selected text:

    "editor.renderWhitespace": "selection"
    

    and

    "workbench.colorCustomizations": {    
      "editorWhitespace.foreground": "#fbff00"
    }
    


    0 讨论(0)
  • 2020-12-04 05:28

    It can also be done via the main menu View -> Render Whitespace

    0 讨论(0)
  • 2020-12-04 05:38

    UPDATE (June 2019)

    For those willing to toggle whitespace characters using a keyboard shortcut, you can easily add a keybinding for that.

    In the latest versions of Visual Studio Code there is now a user-friendly graphical interface (i.e. no need to type JSON data etc) for viewing and editing all the available keyboard shortcuts. It is still under

    File > Preferences > Keyboard Shortcuts (or use Ctrl+K Ctrl+S)

    There is also a search field to help quickly find (and filter) the desired keybindings. So now both adding new and editing the existing keybindings is much easier:


    Toggling whitespace characters has no default keybinding so feel free to add one. Just press the + sign on the left side of the related line (or press Enter, or double click anywhere on that line) and enter the desired combination in the pop-up window.

    And if the keybinding you have chosen is already used for some other action(s) there will be a convenient warning which you can click and observe what action(s) already use your chosen keybinding:

    As you can see, everything is very intuitive and convenient.
    Good job, Microsoft!


    Original (old) answer

    For those willing to toggle whitespace characters using a keyboard shortcut, you can add a custom binding to the keybindings.json file (File > Preferences > Keyboard Shortcuts).

    Example:

    // Place your key bindings in this file to overwrite the defaults
    [
        {
            "key": "ctrl+shift+i",
            "command": "editor.action.toggleRenderWhitespace"
        }
    ]
    

    Here I have assigned a combination of Ctrl+Shift+i to toggle invisible characters, you may of course choose another combination.

    0 讨论(0)
  • 2020-12-04 05:39

    It is not a boolean anymore. They switched to an enum. Now we can choose between: none, boundary, and all.

    // Controls how the editor should render whitespace characters,
    // posibilties are 'none', 'boundary', and 'all'.
    // The 'boundary' option does not render single spaces between words.
    "editor.renderWhitespace": "none",
    

    You can see the original diff on GitHub.

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