How to set the tab size and indentation in Visual Studio Code

Change Editor: Tab Size in VS Code Settings to set the tab width. Editor: Insert Spaces controls whether the editor uses spaces for indentation, and Editor: Detect Indentation lets existing files override the defaults. Convert indentation separately to change stored tab characters.

Tab width and inserted characters

editor.tabSize defines tab stops in columns. The indent step follows it by default. A separate numeric editor.indentSize can change the step.

Enable editor.insertSpaces to indent with spaces, or disable it to use tab characters. Changing this option affects new indentation. It leaves existing characters intact.

Set a user or workspace default

  1. Open the Command Palette and run Preferences: Open Settings (UI). Choose User for your default or Workspace for the open project.
  2. Find editor.tabSize and set it to 2. Enable editor.insertSpaces for a project that uses spaces.
  3. Turn off editor.detectIndentation if you need these values to apply even when the file follows another style. Reopen a file and check the right side of the status bar for Spaces: 2.

For a single-folder workspace, merge the following entries into .vscode/settings.json. Keep one outer pair of braces and update existing keys where necessary.

{
  "editor.tabSize": 2,
  "editor.insertSpaces": true,
  "editor.detectIndentation": false
}

This example uses two spaces and disables detection for the project. Record the old values before trying it. Revert only those entries or remove the keys you added.

Change one file and convert its indentation

  1. Click Spaces or Tab Size on the right side of the status bar. Pick Indent Using Spaces or Indent Using Tabs, then select a width for new indentation in that file.
  2. To rewrite existing indentation, choose Convert Indentation to Spaces or Convert Indentation to Tabs. Try it on a copy first, then convert and save the target file.
  3. Temporarily set editor.renderWhitespace to all to see tabs and spaces. Restore its previous value once you have checked the changes.

For example, with a tab size of 2, converting a line such as return 1; with one leading tab replaces that tab with two spaces.

Microsoft’s VS Code indentation guide

When the setting does not take effect

Read the status bar first. With detection enabled, the file’s indentation can override your defaults. If the values differ with detection off, check Workspace settings, language overrides such as [javascript], and any separate editor.indentSize.

If indentation changes only after saving, inspect the formatter’s project settings. Its indentation rules may differ from the editor’s.

VS Code settings precedence