Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VS Code Alignment Settings Explained

· 5 min read · Home Tech

In this article
  1. What "Alignment" Means in VS Code
  2. The Core Indentation Settings
  3. Formatting and Alignment Through Extensions
  4. How to Set Up Consistent Alignment
  5. Common Alignment Problems and Fixes
  6. Alignment Versus Formatting: A Useful Distinction
  7. Frequently Asked Questions

VS Code does not have a single "alignment" setting; instead, alignment is controlled through a handful of editor settings and the formatting behavior of your installed language extension. The most relevant options are editor.formatOnSave, editor.formatOnType, editor.tabSize, editor.insertSpaces, editor.detectIndentation, and editor.defaultFormatter, which together determine how code is indented, spaced, and aligned when you type or save a file.

What "Alignment" Means in VS Code

People use the word alignment loosely. In VS Code it can refer to several distinct things: indentation (the leading whitespace on each line), column alignment of assignments or trailing comments, alignment of code blocks after formatting, or even the visual alignment of the minimap and editor gutter. VS Code itself only directly handles indentation and whitespace through its built-in settings. Everything else, such as aligning the equals signs in a block of variable assignments or aligning trailing comments, is the job of a formatter or an extension.

This distinction matters because searching for an "alignment" setting in the Settings UI often turns up nothing useful. The settings you actually need are named after indentation, formatting, and whitespace.

The Core Indentation Settings

These built-in settings control the whitespace VS Code inserts and how it interprets existing files. They are the foundation of any alignment behavior.

  • editor.tabSize — how many columns a tab represents, default 4. Set it to 2 for many JavaScript, TypeScript, and YAML projects.
  • editor.insertSpaces — when true, pressing Tab inserts spaces instead of a tab character. Most style guides expect spaces.
  • editor.detectIndentation — when true (the default), VS Code guesses tab size and space usage from the file's existing content and overrides your settings for that file. This is a common source of confusion when your configured tab size appears to be ignored.
  • editor.trimAutoWhitespace — removes trailing whitespace on lines you edit, which keeps diffs clean.
  • editor.renderWhitespace — can be set to none, boundary, selection, trailing, or all so you can actually see the spaces and tabs causing misalignment.

You can change these per workspace by editing .vscode/settings.json in the project root, which is the recommended way to keep a team consistent. Per-language overrides are also possible using a language-specific block such as "[python]": { "editor.tabSize": 4 }.

Formatting and Alignment Through Extensions

Real code alignment, meaning lining up operators, colons in object literals, or trailing comments, is performed by formatters. VS Code ships with no opinionated formatter for most languages, so alignment quality depends on what you install.

  • Prettier handles JavaScript, TypeScript, CSS, JSON, Markdown, and more. It deliberately does not align assignment operators; it normalizes indentation and spacing instead.
  • Black does the same for Python, rejecting manual alignment in favor of consistent formatting.
  • clang-format supports alignment options such as AlignConsecutiveAssignments and AlignTrailingComments for C, C++, and related languages.
  • EditorConfig support is built in; an .editorconfig file can enforce indent style, indent size, and trailing whitespace across editors, not just VS Code.

Once a formatter is installed, set editor.defaultFormatter to choose it and enable editor.formatOnSave so alignment is applied automatically. Use the Format Document command (Shift+Alt+F on Windows and Linux, Shift+Option+F on macOS) to format on demand.

How to Set Up Consistent Alignment

  1. Decide the indentation style for the project: spaces or tabs, and how many columns.
  2. Open Settings (Ctrl+, or Cmd+, on macOS) and search for tab size to set editor.tabSize and editor.insertSpaces.
  3. Turn off editor.detectIndentation if you want your settings to always win over the file's existing whitespace.
  4. Install the formatter appropriate to your language and set it as the default formatter.
  5. Enable editor.formatOnSave and optionally editor.formatOnType.
  6. Add a .editorconfig file and a workspace settings.json so every contributor gets the same result.
  7. Set editor.renderWhitespace to boundary or all while troubleshooting so misaligned whitespace is visible.

Common Alignment Problems and Fixes

The most frequent complaint is that changing the tab size appears to do nothing. In almost every case editor.detectIndentation is overriding the setting based on the file's existing content. Disable detection or run the Convert Indentation to Spaces or Convert Indentation to Tabs command from the Command Palette to normalize the file first.

Another common issue is mixed tabs and spaces producing ragged columns. Turning on whitespace rendering makes this obvious, and the formatter will usually resolve it. If a formatter keeps undoing manual alignment, that is intentional: most modern formatters treat manual operator alignment as noise. If your team requires aligned assignments, choose a formatter that supports it, such as clang-format, rather than fighting the formatter by hand.

Finally, alignment can look wrong because of the font. Proportional fonts break column alignment; a monospaced font is required for code columns to line up reliably. VS Code's default fonts are monospaced, so this usually only appears after a user changes the font family.

Alignment Versus Formatting: A Useful Distinction

Indentation settings tell VS Code what whitespace to insert when you press Tab or Enter. Formatting rewrites the whole file according to a style. Alignment of operators and comments sits in the formatting layer, not the editor settings layer. Understanding this prevents wasted time searching for a setting that does not exist. If you want a specific alignment rule, look for a formatter option or a dedicated extension rather than an editor preference.

Frequently Asked Questions

Where is the alignment setting in VS Code?

There is no setting literally called alignment. Use editor.tabSize, editor.insertSpaces, and editor.detectIndentation for indentation, and a formatter for operator or comment alignment.

Why does VS Code ignore my tab size?

Because editor.detectIndentation is enabled by default and infers indentation from the open file. Disable it or convert the file's indentation to normalize it.

How do I align equals signs in VS Code?

VS Code does not do this natively. Install a formatter that supports consecutive assignment alignment, or an alignment extension for your language, and run Format Document.

Can I set different indentation per language?

Yes. Use a language-scoped block in settings, such as "[javascript]": { "editor.tabSize": 2 }, so each language follows its own convention.

Does Prettier align code?

Prettier normalizes indentation and spacing but deliberately does not align assignment operators or trailing comments. That is a design choice, not a bug.

How do I see the whitespace causing misalignment?

Set editor.renderWhitespace to boundary or all. Tabs and spaces then appear as visible markers, making the cause obvious.