How to change indentation in Visual Studio Code?

2019-01-21 05:21发布

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's available as a setting but under a different name as I can't find anything related to indentation.

8条回答
Lonely孤独者°
2楼-- · 2019-01-21 05:34

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
查看更多
别忘想泡老子
3楼-- · 2019-01-21 05:36

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

查看更多
叼着烟拽天下
4楼-- · 2019-01-21 05:38

In the toolbar in the bottom right corner you will see a item that looks like the following: enter image description here After clicking on it you will get the option to indent using either spaces or tabs. After selecting your indent type you will then have the option to change how big an indent is. In the case of the example above, indentation is set to 4 space characters per indent. If tab is selected as your indentation character then you will see Tab Size instead of Spaces

If you want to have this apply to all files and not on an idividual file basis, override the editor.tabSize and editor.insertSpaces settings in either User Settings or Workspace Settings depending on your needs

查看更多
老娘就宠你
5楼-- · 2019-01-21 05:43

Adding on: yes, you can use the bottom-right UI to configure the space settings. But if you have existing code that's not formatted to the new spacing, then you can right-click anywhere within the file and click Format Document. Took me a while to figure this out until I stumbled on this issue.

Format Document menu

查看更多
6楼-- · 2019-01-21 05:44

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

查看更多
祖国的老花朵
7楼-- · 2019-01-21 05:49

To change the indentation based on programming language, follow one of these ways:

  • Add this to your settings (Ctrl+,):
    (example for TypeScript specific settings):

    "[typescript]": {
        "editor.tabSize": 2
    }
    

OR

  • Ctrl+Shift+P
  • Preferences: Configure language specific settings... (command id: workbench.action.configureLanguageBasedSettings)
  • Select programming language
  • Add code as above.

See also: VS Code Docs

查看更多
登录 后发表回答