Visual Studio Code
Indentation
Coding Techniques
Programming
Software Tips

How to change indentation and tab size in VS Code

Codemia Knowledge Hub ·

Click the indentation indicator in the bottom-right status bar of VS Code (it reads something like Spaces: 4 or Tab Size: 2). From there you can switch between tabs and spaces, change the width, or convert the file's existing indentation. For defaults, set editor.tabSize and editor.insertSpaces in settings.json.

There are three separate things here: tabs vs spaces, the column width, and converting existing files. VS Code handles all three, but from different places.

Changing indentation for the current file

Click the Spaces: 4 indicator in the status bar to get:

  • Indent Using Spaces: switch to spaces and choose a width.
  • Indent Using Tabs: switch to tab characters and choose a display width.
  • Convert Indentation to Spaces: rewrite existing tabs as spaces in this file.
  • Convert Indentation to Tabs: rewrite existing spaces as tabs in this file.

The same commands are in the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) under "indentation":

text
1> Convert Indentation to Spaces
2> Convert Indentation to Tabs
3> Indent Using Spaces
4> Indent Using Tabs
5> Change Tab Display Size

"Indent Using" changes how new indentation is inserted. "Convert Indentation" rewrites the whitespace already in the file.

Default tab size in settings.json

json
1{
2  "editor.insertSpaces": true,
3  "editor.tabSize": 4,
4  "editor.detectIndentation": false
5}
SettingEffect
editor.insertSpacestrue uses spaces, false uses tab characters
editor.tabSizeNumber of columns per indentation level
editor.detectIndentationWhen true, VS Code auto-detects from file content

With editor.detectIndentation at true (the default), VS Code reads each file and overrides tabSize and insertSpaces to match it. That helps in mixed-style repositories but is confusing when you expect your settings to apply. Set it to false to make your settings win.

Per-language indentation

Python uses 4 spaces, many JavaScript style guides use 2, and Go uses tabs. Override per language:

json
1{
2  "[python]": {
3    "editor.tabSize": 4,
4    "editor.insertSpaces": true
5  },
6  "[javascript]": {
7    "editor.tabSize": 2,
8    "editor.insertSpaces": true
9  },
10  "[go]": {
11    "editor.insertSpaces": false,
12    "editor.tabSize": 4
13  },
14  "[makefile]": {
15    "editor.insertSpaces": false
16  }
17}

Language settings override global ones but are overridden by .editorconfig and formatter extensions.

EditorConfig

Many projects ship an .editorconfig to keep formatting consistent across editors. VS Code respects it (with built-in support or the EditorConfig extension).

ini
1root = true
2
3[*]
4indent_style = space
5indent_size = 4
6end_of_line = lf
7charset = utf-8
8trim_trailing_whitespace = true
9
10[*.js]
11indent_size = 2
12
13[*.go]
14indent_style = tab
15
16[Makefile]
17indent_style = tab

When present, .editorconfig beats settings.json, so project rules override personal preferences.

Formatters have the final word

Prettier, ESLint, Black or gofmt decide indentation when you save or format. With Prettier set to 2 spaces:

json
1{
2  "tabWidth": 2,
3  "useTabs": false
4}

Prettier rewrites the file to 2 spaces on format, whatever VS Code says. The priority chain:

text
1Formatter (Prettier, Black, etc.)
2  > .editorconfig
3    > Language-specific VS Code settings
4      > Global VS Code settings
5        > Auto-detected indentation (if detectIndentation is true)

Most "my indentation keeps changing back" problems come from a forgotten formatter or .editorconfig.

Workspace vs user settings

  • User Settings apply to all projects: ~/.config/Code/User/settings.json (Linux), ~/Library/Application Support/Code/User/settings.json (macOS), or %APPDATA%\Code\User\settings.json (Windows).
  • Workspace Settings apply to the current project: .vscode/settings.json in the project root.
json
1// .vscode/settings.json (workspace-level)
2{
3  "editor.tabSize": 2,
4  "editor.insertSpaces": true
5}

Workspace settings override user settings. Commit .vscode/settings.json so the whole team gets the same indentation.

Indentation shortcuts

ActionWindows/LinuxmacOS
IndentTabTab
OutdentShift+TabShift+Tab

To fix indentation across a whole file after changing settings:

  1. Select all (Ctrl+A / Cmd+A).
  2. Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P).
  3. Run Format Document (if a formatter is installed) or Reindent Lines.

Converting many files at once

For a project-wide change (say, tabs to 2 spaces), the command line is faster than VS Code.

Using expand (Unix):

bash
# Convert tabs to 2 spaces in all JavaScript files
find ./src -name "*.js" -exec sh -c 'expand -t 2 "$1" > "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;

Using Prettier:

bash
npx prettier --write --tab-width 2 --use-tabs false "src/**/*.js"

Why indentation does not change as expected

  • Changing editor.tabSize does not convert tabs. It only changes how tab characters display. Run "Convert Indentation to Spaces" to rewrite the file.
  • A formatter or .editorconfig is enforcing a standard. Look for .editorconfig, .prettierrc, pyproject.toml or similar before overriding.
  • editor.detectIndentation is on. VS Code follows the file's existing style and silently ignores your defaults.
  • Mixed tabs and spaces get committed when teammates use different settings. An .editorconfig or a pre-commit formatting hook prevents it.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms