Visual Studio Code - Convert spaces to tabs

254,563

Solution 1

There are 3 options in .vscode/settings.json:

// 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:

To access these settings, you can open File » Preferences » Settings, click the Manage cog icon at the bottom left, or use the keyboard shortcut:

CTRL+, (Windows, Linux)

+, (Mac)

Update:

Now you have an alternative to editing those options manually.
Click on selector Spaces:4 at the bottom-right of the editor:
 Ln44, Col .  [Spaces:4] . UTF-8 with BOM . CTRLF . HTML . :)

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.

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

enter image description here

Solution 3

Ctrl+Shift+P, then "Convert Indentation to Tabs"

Solution 4

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

Editor: Insert Spaces

  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"
  1. Save it (Go to FileSave or just press Ctrl + S)
  2. Reload Visual Studio Code (Press F1 ➤ type reload window ➤ press Enter)

Solution 5

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,
// Config the editor that making the "space" instead of "tab"
"editor.insertSpaces": true,
// Configure editor settings to be overridden for [html] language.
"[html]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2,
    "editor.autoIndent": false
}
Share:
254,563

Related videos on Youtube

Matan Yadaev
Author by

Matan Yadaev

Software engineer at Showdigs.com

Updated on July 08, 2022

Comments

  • Matan Yadaev
    Matan Yadaev 6 months

    I have both TypeScript and HTML files in my project, in both files tabs are converted to spaces.

    I want to turn the auto-conversion off and make sure that my project has only tabs.

    Edit:

    With this setting it seems to work in HTML files but not in TypeScript files.

    {
      "editor.insertSpaces": false
    }
    
    • Joshua Schlichting
      Joshua Schlichting over 2 years
      It's beyond me why IDE's default to converting tabs to spaces. If I wanted spaces, I'd tap the space bar 4 times for every indent.... like a savage.
  • Avijeet
    Avijeet over 5 years
    Oh yes, this is was the actual issue for me. spend a lot of time tinkering around setting in VS code. finally disabling this extension solved the problem.
  • Kyle Vassella
    Kyle Vassella over 4 years
    For me, this only changes these settings for the particular file I'm currently viewing. The other files in the project keep their old settings. How would you set it universally?
  • Alex Logvin
    Alex Logvin over 3 years
    If you want spaces instead of tabs, modify formatter.json file: put one space in any quotation mark instead of \t (So "\t" became " "), and put 4 where you see 1. So you come might be like this "indent_size": 4, "indent_char": " " "indent_size": 4, "indentCharacter": " ", "indent_char": " ", "indent_size": 4, "indent_char": " ", "indent_character": " "
  • cossacksman
    cossacksman about 3 years
    I usually replace all my files at once, if I need to, by doing a project search for the simple regex ` {4}` and replace with \t (for tabs). Usually also glance over the matches to make sure nothing will break like multi-line string literals, etc. Obviously apply these settings so that your new indentations are the desired characters, too!
  • Rin and Len
    Rin and Len about 3 years
    The RELOAD WINDOW step was what I was missing. Frustrating to have gone the process of checking boxes and fiddling with settings 3 times for no changes to take, when all I had to do was reload window. Thanks for the tip!
  • Vijender Kumar over 2 years
    How do I run this command for all files in my workspace.
  • Farid Alijani
    Farid Alijani almost 2 years
    how do I do >Convert indentation to Tabs permanently for all my files and projects in VSCode?
  • v-andrew
    v-andrew almost 2 years
    @FäridAlijani, @ VijenderKumar I don't think it is possible using VS Code
  • dbrane
    dbrane over 1 year
    Is it possible to do this for a selection and not the whole file?
  • Matt Wyndham over 1 year
    is it possible to do this for a whole repo and not just one file?
  • Trutane
    Trutane about 1 year
    @dbrane Yes. If you highlight a block of code in a file, the conversion applies just within the selection. Btw, on Mac the key combo is ⌥ + P (alt/option + P).