Automatically Convert Tabs to Spaces in VS Code
Learn how to make VS Code convert tabs to spaces automatically, including editor settings, language overrides, and save-time formatting behavior.
Define the alternatives
VS Code can handle indentation in two main ways: insert tab characters or insert spaces when you press Tab. The automatic conversion task usually means changing newly typed indentation from tabs to spaces, then optionally cleaning up existing tab characters in a file.
There are two related workflows. Editor-level indentation controls what VS Code inserts while you type. A separate set of formatting or file-conversion commands can replace existing tabs with spaces across the current document.
This article focuses on files edited in the desktop version of VS Code. Web-based or remote workspaces use the same settings model when the editor supports it, but extension behavior can vary by environment.
Compare meaningful differences
The main editor setting is Editor: Insert Spaces. When enabled, pressing Tab inserts the number of spaces defined by Editor: Tab Size. When disabled, VS Code inserts a tab character instead. This setting can be changed globally or per language.
VS Code can also detect indentation from an open file. If Editor: Detect Indentation is enabled, the detected style may override your configured Insert Spaces setting for that file. This is useful for preserving mixed projects, but it can make automatic conversion appear not to work.
For existing files, the command Convert Indentation to Spaces changes indentation to spaces across the document. A separate Replace action can swap literal tab characters for spaces. These commands affect the current file rather than changing future typing behavior.
Language overrides apply only to a specified language identifier, such as javascript or python. Global settings apply unless a more specific override exists. Workspace settings apply to an open folder, while user settings apply across all windows.
Practical trade-offs
Enabling Insert Spaces gives consistent visual indentation across viewers and tools that do not render tab widths the same way. The trade-off is that files become larger in character count than the same indentation stored as tabs, though the practical file-size difference is usually minor.
Keeping Detect Indentation enabled is safer for mixed repositories because VS Code does not silently rewrite the indentation style while you edit. If you disable it and force Insert Spaces, a file with existing tabs can end up with mixed indentation until you run a conversion command.
Running Convert Indentation to Spaces changes the entire open file immediately. This produces a larger visible diff in version-controlled projects, especially if the file previously used tabs. Reviewing the change before saving is advisable.
Some language servers and formatters apply their own indentation defaults. In those cases, VS Code editor settings may control typing while the formatter controls the final layout. Version-specific behavior should be checked in the extension documentation.
Choose for the use case
If you want every new line to use spaces by default, enable Editor: Insert Spaces in user settings and set Editor: Tab Size to your preferred width.
If only Python, YAML, or another language should use spaces, create a language override instead of changing the global setting. This preserves tab-based indentation for other file types such as Makefiles or Go source.
If an existing file already contains tab indentation, run Convert Indentation to Spaces after opening it. For stricter replacement of literal tab characters, use the Replace command with regular expressions enabled, but first make a backup or check version control so the change can be reviewed.
If a project must remain compatible with tools that require tabs, do not force spaces globally. Use a workspace setting or language override only where spaces are safe, and leave Detect Indentation enabled when working in repositories with mixed styles.