How to change indentation in Visual Studio Code?

前端 未结 14 1567
臣服心动
臣服心动 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:32

    In the toolbar in the bottom right corner you will see a item that looks like the following: 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: Tab Size and Editor: Insert Spaces settings in either User Settings or Workspace Settings depending on your needs

    Edit 1

    To get to your user or workspace settings go to Preferences -> Settings. Verify that you are on the User or Workspace tab depending on your needs and use the search bar to locate the settings. You may also want to disable Editor: Detect Indentation as this setting will override what you set for Editor: Insert Spaces and Editor: Tab Size when it is enabled

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

    Simplified explanation with pictures for those that googled "Change indentation in VS Code"

    Step 1: Click on Preferences > Settings

    Step 2: The setting you are looking for is "Detect Indentation", begin typing that. Click on "Editor: Tab Size"

    Step 3: Scroll down to "Editor: Tab Size" and type in 2 (or whatever you need).



    Changes are automatically saved

    Example of my changes

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

    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

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

    How to turn 4 spaces indents in all files in VS Code to 2 spaces

    • Open file search
    • Turn on Regular Expressions
    • Enter: ( {2})(?: {2})(\b|(?!=[,'";\.:\*\\\/\{\}\[\]\(\)])) in the search field
    • Enter: $1 in the replace field

    How to turn 2 spaces indents in all files in VS Code to 4 spaces

    • Open file search
    • Turn on Regular Expressions
    • Enter: ( {2})(\b|(?!=[,'";\.:\\*\\\/{\}\[\]\(\)])) in the search field
    • Enter: $1$1 in the replace field

    NOTE: You must turn on PERL Regex first. This is How:

    • Open settings and go to the JSON file
    • add the following to the JSON file "search.usePCRE2": true

    Hope someone sees this.

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

    To change the indentation based on programming language:

    1. Open the Command Palette (CtrlShiftP | macOS: P)
    2. Preferences: Configure language specific settings... (command id: workbench.action.configureLanguageBasedSettings)
    3. Select programming language (for example TypeScript)
    4. Add this code:

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

    See also: VS Code Docs

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

    You can change this in global User level or Workspace level.

    Open the settings: Using the shortcut Ctrl , or clicking File > Preferences > Settings as shown below.

    Then, do the following 2 changes: (type tabSize in the search bar)

    1. Uncheck the checkbox of Detect Indentation
    2. Change the tab size to be 2/4 (Although I strongly think 2 is correct for JS :))

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