Spaces to tabs in Visual Studio Code

2019-01-30 16:58发布

问题:

I writing typescript and html files, and the tabs become to spaces. It's the first time I am using Visual Studio Code.

I tried to change the settings and restart, but nothing changed.

The settings i configured:

// Place your settings in this file to overwrite default and user settings.
{
    "editor.insertSpaces": false
}

I tried google it and I found that I did the right step when I added insertSpaces to true, but it's not changing my preferences...

EDIT 1:

I found that the tabs work in html files, but not ts files...

回答1:

There are 3 options:

// The number of spaces a tab is equal to.
"editor.tabSize": 4,

// Insert spaces when pressing Tab.
"editor.insertSpaces": true,

// When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents.
"editor.detectIndentation": true

editor.detectIndentation detects it from your file, you have to disable it. If it didn't help, check that you have no settings with higher priority. For example when you save it to User settings it could be overwritten by Workspace settings which are in your project folder.

Update:
Now you have alternative to editing those options manually.
Click on selector Spaces:4 at the bottom-right of the editor:

When you want to convert existing ws to tab, install extension from Marketplace
EDIT:
To convert existing indentation from spaces to tabs hit Ctrl+Shift+P and type:

>Convert indentation to Tabs

This will change the indentation for your document based on the defined settings to Tabs.



回答2:

To change tab settings, click the text area right to the Ln/Col text in the status bar on the bottom right of vscode window.

The name can be Tab Size or Spaces.

A menu will pop up with all available actions and settings.



回答3:

Ctrl + shift + P then "Convert indentation to tabs"



回答4:

In my case, the problem was JS-CSS-HTML Formatter extension installed after january update. The default indent_char property is space. I uninstalled it and the weird behavior stops.



回答5:

Below settings are worked well for me,

"editor.insertSpaces": false,
"editor.formatOnSave": true, // only if you want auto fomattting on saving the file
"editor.detectIndentation": false

Above settings will reflect and applied to every files. You don't need to indent/format every file manually.



回答6:

If you want to use tabs instead of spaces

Try this:

  1. Go to FilePreferencesSettings or just press Ctrl + ,
  2. In the Search settings bar on top insert editor.insertSpaces
  3. You will see something like this: Editor: Insert Spaces and it will be probably checked. Just uncheck it as show in image below

  1. Reload Visual Studio Code (Press F1 ➤ type reload window ➤ press Enter)

If it doesn't worked try this:

It's probably because of installed plugin JS-CSS-HTML Formatter

(You can check it by going to FilePreferencesExtensions or just pressing Ctrl + Shift + X, in the Enabled list you will find JS-CSS-HTML Formatter)

If so you can modify this plugin:

  1. Press F1 ➤ type Formatter config ➤ press Enter (it will open the file formatter.json)
  2. Modify the file like this:

     4|    "indent_size": 1,
     5|    "indent_char": "\t"
    ——|
    24|    "indent_size": 1,
    25|    "indentCharacter": "\t",
    26|    "indent_char": "\t",
    ——|
    34|    "indent_size": 1,
    35|    "indent_char": "\t",
    36|    "indent_character": "\t"
    
  3. Save it (Go to FileSave or just press Ctrl + S)

  4. Reload Visual Studio Code (Press F1 ➤ type reload window ➤ press Enter)


回答7:

Check this from official vscode setting:

  // Controls whether `editor.tabSize#` and `#editor.insertSpaces` will be automatically detected when a file is opened based on the file contents.
  "editor.detectIndentation": true,

  // The number of spaces a tab is equal to. This setting is overridden based on the file contents when `editor.detectIndentation` is on.
  "editor.tabSize": 4,

  // Configure editor settings to be overridden for [html] language.
  "[html]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2,
    "editor.autoIndent": false
  }


回答8:

File -> Preferences -> Settings (or just press Ctrl+,, and search for spaces, then just deactivate this option:

I had to reopen the file so the changes take effect.