How to change indentation in Visual Studio Code?

前端 未结 14 1571
臣服心动
臣服心动 2020-12-02 03:47

For every typescript file visual studio code uses an auto indentation of 8 spaces. This is a bit too much for my taste but I can\'t find where to change it.

Maybe it

相关标签:
14条回答
  • 2020-12-02 04:42

    You might also want to set the editor.detectIndentation to false, in addition to Elliot-J's answer.

    VSCode will overwrite your editor.tabSize and editor.insertSpaces settings per file if it detects that a file has a different tab or spaces indentation pattern. You can run into this issue if you add existing files to your project, or if you add files using code generators like Angular Cli. The above setting prevents VSCode from doing this.

    0 讨论(0)
  • 2020-12-02 04:44

    Setting the indentation in preferences isn't allways the solution. Most of the time the indentation is right except you happen to copy some code code from other sources or your collegue make something for you and has different settings. Then you want to just quickly convert the indentation from 2 to 4 or the other way round.

    That's what this vscode extension is doing for you

    0 讨论(0)
  • 2020-12-02 04:47

    To set all existing files and new files to space identation to 2 just put it in your settingns.json (in the root of json):

    "[typescript]": {
            "editor.defaultFormatter": "vscode.typescript-language-features",
            "editor.tabSize": 2,
            "editor.insertSpaces": true,
            "editor.detectIndentation":false
     }
    

    you can add the language type of the configuration:

    "[javascript]": {
        "editor.tabSize": 2,
        "editor.insertSpaces": true,
        "editor.detectIndentation":false
    } 
    
    0 讨论(0)
  • 2020-12-02 04:49

    Code Formatting Shortcut:

    VSCode on Windows - Shift + Alt + F

    VSCode on MacOS - Shift + Option + F

    VSCode on Ubuntu - Ctrl + Shift + I

    You can also customize this shortcut using preference setting if needed.

    column selection with keyboard Ctrl + Shift + Alt + Arrow

    0 讨论(0)
  • 2020-12-02 04:51

    In my case "EditorConfig for VS Code" extention is overriding VSCode settings. If you have it installed, then check .editorconfig file in the root folder of the project.

    Here is an example config. The "indent_size" sets the number of spaces for a tab.

    # editorconfig.org
    root = true
    
    [*]
    indent_style = space
    indent_size = 4
    end_of_line = lf
    charset = utf-8
    trim_trailing_whitespace = true
    insert_final_newline = true
    
    [*.md]
    trim_trailing_whitespace = false
    
    0 讨论(0)
  • 2020-12-02 04:52

    Problem: The accepted answer does not actually fix the indentation in the current document.

    Solution: Run Format Document to re-process the document according to current (new) settings.

    Problem: The HTML docs in my projects are of type "Django HTML" not "HTML" and there is no formatter available.

    Solution: Switch them to syntax "HTML", format them, then switch back to "Django HTML."

    Problem: The HTML formatter doesn't know how to handle Django template tags and undoes much of my carefully applied nesting.

    Solution: Install the Indent 4-2 extension, which performs indentation strictly, without regard to the current language syntax (which is what I want in this case).

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