Why VS Code Inserts Spaces by Default
VS Code detects indentation style per file and, when "editor.detectIndentation" is true, it automatically switches to spaces if the file contains them. This behavior applies to all languages unless overridden.
Global vs. Language‑Specific Settings
Settings in the global user settings.json apply to every file. Workspace settings (.vscode/settings.json) override them for the current project. Language overrides use the language id, e.g. "typescript" or "html".
"typescript": {
"editor.insertSpaces": false,
"editor.tabSize": 4
}
Turn Off Space Insertion for All Files
Set the following in either user or workspace settings to enforce tabs everywhere:
"editor.insertSpaces": false,
"editor.tabSize": 4,
"editor.detectIndentation": false
Fixing TypeScript’s Auto‑Indentation
TypeScript files often inherit settings from the TypeScript language server. Adding a language override and disabling detection ensures tabs are used:
1. Add the "typescript" block from the previous section. 2. If formatting still inserts spaces, disable formatOnSave or set "editor.defaultFormatter" to null for TypeScript.
"typescript": {
"editor.insertSpaces": false,
"editor.tabSize": 4,
"editor.formatOnSave": false
}
Verify and Clean Existing Files
After changing settings, run:
- Find all spaces: Ctrl+Shift+F, search for "^\s+". - Replace with tabs: use the replace dialog with the tab character (Ctrl+V, then press Tab).
You can also add an .editorconfig file to lock the style across editors:
[*]
indent_style = tab
indent_size = 4
Takeaway: Set "editor.insertSpaces": false, disable detection, and apply language overrides to keep tabs in all project files.
People also ask
How do I apply the tab setting only to the current workspace?
Edit .vscode/settings.json and add the desired settings; they override user settings for that project.
Will disabling "editor.detectIndentation" affect other projects?
Only the workspace where you set it; other workspaces use their own settings.
Can I enforce tabs using an editorconfig file?
Yes, add an .editorconfig with "indent_style = tab" and VS Code will honor it.
Inspired by a public discussion on Stack Overflow. This article is an original explanation for learners.