VS Code

How to Comment in VSCode: Single Line, Block, and Multi-Line

How to Comment in VSCode: Single Line, Block, and Multi-Line

Set editor.formatOnSave to true and point editor.defaultFormatter at the formatter you want, and VSCode tidies the file every time you save.

If you’d rather run it yourself, Format Document is the manual version. That’s Shift+Alt+F on Windows and Shift+Option+F on macOS. Linux uses Ctrl+Shift+I.

None of this is switched on out of the box. As of November 2026, the VS Code documentation lists format on type, format on save and format on paste as all off by default.

The built-in formatters cover only JavaScript, TypeScript, JSON, HTML and CSS. A Python file needs a formatter extension before either setting has any effect.

How do you format code in VSCode manually?

Run Format Document on the active file with Shift+Alt+F (Windows), Shift+Option+F (macOS) or Ctrl+Shift+I (Linux).

Format Selection does the same thing for highlighted text only. Both commands show up in the Command Palette and in the editor context menu.

Format Document

One pass rewrites indentation, spacing and line breaks across the whole file. What the result looks like depends on the language and on whichever formatter extension is active.

If no formatter exists for the language, VSCode shows a dialog offering to install one. For Python files, that suggestion can be autopep8.

Format Selection

This is the one to reach for when a single messy function needs cleaning and the rest of the file should stay exactly as it is.

The shortcut is Ctrl+K followed by Ctrl+F on Windows and Linux, and Cmd+K followed by Cmd+F on macOS.

Support varies by formatter. The Prettier extension limits Format Selection to javascript, javascriptreact, typescript, typescriptreact, json, graphql and handlebars files.

Built-in formatters and the install prompt

Everything outside the built-in five needs an extension. Search the Extensions view for “category:formatters” to list the Marketplace’s formatter category, or browse the wider range of VS Code extensions if you want to compare options first.

A built-in formatter can be switched off per language, for example with “html.format.enable”: false, when an extension should handle that language instead.

Which formatter fits each language?

Prettier handles the web stack. Python goes to Black or Ruff, while Go and C/C++ get theirs from the language extension. Pick by language first, then go with whatever the project already uses.

LanguageFormatterExtension ID
JavaScript, TypeScript, CSS, HTML, JSON, Markdown, YAMLPrettieresbenp.prettier-vscode
PythonBlack Formatterms-python.black-formatter
PythonRuffcharliermarsh.ruff
Gogofmtgolang.go
C and C++clang-formatms-vscode.cpptools

Prettier or the built-in web formatter

If the project already has a .prettierrc, Prettier is the easy call, since the config file travels with the repository. The built-in formatter only makes sense when you want zero installs. Honestly, on most web projects I’d install Prettier and stop thinking about it.

Options and full setup live in this guide to using Prettier in VSCode.

Prettier handles JavaScript, TypeScript, CSS, HTML, JSON, Vue, GraphQL, Markdown and YAML with one tool. The extension bundles Prettier 3.x, though a local install in node_modules takes precedence over the bundled copy. The catch is that once any local config file such as .prettierrc exists, the VS Code Prettier settings are not used, so changing them in the editor does nothing.

The built-in formatter needs no install for JavaScript, TypeScript, JSON, HTML and CSS. Markdown, YAML and Vue aren’t covered, so those still need an extension.

Python: Black Formatter or Ruff

Black is the one most Python teams already know. The Black Formatter listing on the Marketplace shows 7,678,890 installs (November 2026).

The extension ships with Black 26.5.1 and uses that bundled binary by default. Astral recommends Ruff extension version 2024.32.0 or later for the best experience with the Ruff Language Server.

Ruff does linting and formatting from a single extension, which is the reason to pick it when you’d rather run one tool. If all you want is a formatter, Black is enough.

Where each formatter trips up

Go is the odd one. Formatting already runs on save by default, so turning it off takes a “[go]” block.

Black defaults importStrategy to useBundled, so set black-formatter.importStrategy to fromEnvironment when the project pins its own Black version. The clang-format that comes bundled with the extension can also differ from the one in CI, which leads to formatting check failures.

How to set the default formatter

YouTube player

Set Editor: Default Formatter, which is the editor.defaultFormatter key, to the extension ID of the formatter you want. The Format Document With command gives you a shortcut to the same setting.

Set it from the editor

  1. Open the Command Palette and run Format Document With.
  2. Choose Configure Default Formatter.
  3. Pick the formatter from the list. VSCode may also show a prompt with a Configure button the first time you format.

Set it in settings.json

Open the file from the Command Palette. The guide on opening settings.json in VSCode covers every route.

For a global default, the entry looks like “editor.defaultFormatter”: “esbenp.prettier-vscode”. To limit it to Python, wrap the key in a “[python]” block and use “ms-python.black-formatter” as the value.

When a per-language setting overrides the global default

Two official sources point in different directions here. The settings description for Editor: Default Formatter says it takes precedence over all other formatter settings, while the VS Code settings documentation states that language-specific editor settings always override non-language-specific ones.

The second statement decides real behavior. The Go extension relies on it, setting language-specific editor settings that take precedence over user and workspace settings.

So if the wrong formatter runs for one language, look for a “[language]” block before touching the global value.

Why an explicit default beats an implicit one

In October 2020, users reported on the VS Code issue tracker that Prettier stopped working, including manual formatting with Shift+Alt+F, until editor.defaultFormatter was set to Prettier. Reports in the same thread tied the change to the 1.50.0 release.

Extension authors say the same thing. A HashiCorp Terraform extension thread explains that several extensions can claim the same language, and VS Code picks between them by activation order, which is non-deterministic.

How to turn on format on save

YouTube player

Set editor.formatOnSave to true. It is off by default, along with format on paste and format on type.

  1. Install a formatter extension for the language, or rely on a built-in one.
  2. Set the default formatter as described above.
  3. Open Settings with Ctrl+, on Windows or Cmd+, on macOS, search for Editor: Format On Save, and tick the checkbox.
  4. Or add “editor.formatOnSave”: true to settings.json.
  5. Save a file with bad indentation and check that it snaps into place.

Turn it on for one language only

Prettier respects the global setting, so you can leave it off and switch it on inside a “[javascript]” block instead.

The Black Formatter extension documents the same pattern for Python, with formatOnSave placed in the “[python]” block next to the default formatter.

Format the whole file or only modified lines

The editor.formatOnSaveMode setting controls whether format on save touches the whole file or only modifications, and it applies only when formatOnSave is true. Modified-lines mode keeps diffs small in legacy codebases.

ModeWhat gets formattedCondition
fileWhole file (the default)None
modificationsChanged lines onlyNo source control means no formatting
modificationsIfAvailableChanged linesFalls back to the entire file without source control

Both modification modes read changed ranges from a repository under source control, then pass those ranges to the formatter.

Support is patchy, though. A November 2023 Ruff extension thread confirmed the modifications mode was not supported yet, pending range formatting, which Ruff added in 2024. In April 2022 a VS Code 1.66.0 user traced a formatting error to Prettier, and it appeared only in the two modification modes.

And outside a repository, the modifications mode formats nothing.

Should you turn on format on paste and format on type?

Turn on format on paste when the formatter supports range formatting, and leave format on type off. Save plus paste covers most projects.

TriggerSettingFormatter must provideVerdict
ManualFormat Document commandDocument formattingAlways available
Saveeditor.formatOnSaveDocument formattingTurn on
Pasteeditor.formatOnPasteRange formattingTurn on if supported
Typeeditor.formatOnTypeOn-type formattingLeave off

Format on paste

VSCode treats a pasted block as a range. Not every formatter supports format on paste, because it has to be able to format a selection or range.

The extension API registers document, range and on-type formatting as three separate providers, which is why a formatter can handle Format Document and still ignore a paste.

To check yours, paste a badly indented block. If the indentation stays wrong, the formatter has no range support.

In the November 2023 Ruff thread, a contributor pointed to missing range formatting support, which would explain a silent paste at the time. Ruff added Format Selection in extension release 2024.10.0 (Ruff 0.2.1 and later), so that gap no longer applies to current versions, and the paste test above is the way to check any other formatter.

Format on type

This is the one trigger worth leaving off.

In October 2025, the .NET team removed default on-type formatting from its C# extension for VS Code, after typing semicolons, braces and other trigger characters caused problems. The pull request cited complaints about it being on by default and at least one known issue that was difficult to resolve. Brace formatting on Enter had also moved to a separate call.

Anyone who still wants it can opt in per language, for example with editor.formatOnType set to true inside a “[csharp]” block.

How do .prettierrc, .editorconfig and settings.json fit together?

For Prettier, a Prettier config file wins over .editorconfig, and .editorconfig wins over VS Code settings. Other formatters follow their own order, so check each tool’s file before editing settings.json.

Which file controls what

Prettier starts in the directory of the file being formatted and searches up the tree for a .prettierrc. It does not support global configuration.

The .editorconfig file holds properties such as indent_style, indent_size, end_of_line, trim_trailing_whitespace and insert_final_newline. Prettier parses it on its own and lets its own config override it.

Black reads the [tool.black] section of pyproject.toml, and it uses only one pyproject.toml per run, without combining several files. In .vscode/settings.json, workspace values take precedence over user values.

For other formatters inside VS Code, the EditorConfig site lists VS Code among the editors that need a plugin.

Mixed repos and the line length gap

Prettier’s documented example uses a max line length of 80, while Black’s default line length is 88.

No single source says it, but the consequence is plain. A repository with JavaScript and Python formatted on defaults ends up with two widths. Pick one number, put it in .editorconfig as max_line_length for Prettier, and repeat it as line-length under [tool.black].

Sharing rules with a team

Commit the formatter config files and the workspace settings file. A DigitalOcean tutorial lists .prettierrc, .prettierignore and a workspace-level .vscode/settings.json as the files that keep contributors consistent.

Once the formatter decides spacing, style nitpicks stop eating into the code review process.

Settings Sync is personal rather than project-level. It carries user settings and installed extensions across machines through a Microsoft or GitHub sign-in, and skips machine-scoped settings.

One limit matters for formatting. Extensions are not synced to or from remote windows such as SSH, dev containers or WSL. A formatter that works locally can vanish inside a container.

Can ESLint fixes run on save with the formatter?

Yes. Add “source.fixAll.eslint”: “explicit” to editor.codeActionsOnSave and keep format on save pointed at your formatter.

A VS Code issue thread notes that formatOnSave always executes after codeActionsOnSave, so ESLint fixes land first and the formatter has the last word.

Values for codeActionsOnSave

Setting it to explicit runs Code Actions only on an explicit save, which is the same as the old true. The old false maps to never, so nothing runs on save at all. And always goes further, running them on Auto Saves triggered by window or focus changes too.

The string values arrived in the September 2023 release (1.83), which announced that the boolean values would be deprecated but still supported them. The November 2023 release (1.85) migrated existing settings to the string values and added Code Actions on Auto Save.

Formatter versus linter

Let the formatter format and the linter lint.

Linting flags rule violations such as unused variables, while a formatter only rewrites layout. The Prettier docs say to use Prettier for formatting concerns and linters for code-quality concerns.

The clash comes from stylistic rules bundled in linters. Prettier’s documentation warns that most stylistic rules are unnecessary with Prettier and might conflict with it.

The recommended fix is eslint-config-prettier, which turns off conflicting rules. Plugins that run Prettier as a lint rule, such as eslint-plugin-prettier, are generally not recommended, since they add editor warnings and slow things down.

Where it breaks

A user in the ESLint extension tracker fixed missed auto-fixes by raising editor.codeActionsOnSaveTimeout from its 750 ms default to 1500 ms.

The config format trips people up as well. ESLint v9 and later use flat config files such as eslint.config.js, and versions older than 8.57 need a legacy .eslintrc. The eslint.useFlatConfig switch defaults to false on 8.57.x and true on 9.x, and it is ignored on v10 and later, where flat config is the only option.

The Python equivalent

Ruff’s formatter does not sort imports, so Astral’s docs run ruff check –select I –fix first and ruff format after.

Inside VS Code, that import step belongs in codeActionsOnSave. One Ruff devcontainer config on GitHub sets “source.organizeImports” to “always” next to its default formatter.

Why is autoformat not working in VSCode?

Start with the basics, meaning a formatter is installed and a default formatter is set. If both check out, the symptom decides the fix.

SymptomCauseFix
Manual format works, save does nothingfiles.autoSave set to afterDelaySwitch to onFocusChange or onWindowChange
“Failed to load module” errorPackages referenced in package.json are not installedRun npm install
“Outdated version of prettier” errorThe project’s Prettier is too old for the extensionUpgrade Prettier
One file never formatsThe path matches .prettierignore, whose default is set by prettier.ignorePathEdit the ignore file, or run Format Document (Forced)

Auto save and format on save disagree

One developer found files slipping through to CI because auto save with afterDelay formatted nothing, and only manual saves triggered formatting. A Stack Overflow answer for VS Code 1.64.2 agrees that onFocusChange or onWindowChange is required.

The sources now conflict on whether that still holds. A Cursor forum request claims VS Code added format on auto save in version 1.97 (January 2025), but the 1.97 release notes mention no such change.

Until release notes document it, treat afterDelay as a setting that skips format on save. Code Actions behave differently, and when set to always they run on auto saves from window or focus changes.

Prettier errors and the trust prompt

The message “This workspace is not trusted. Using the bundled version of prettier” means plugins and local modules are not loading, because they need a trusted workspace. Trust the folder if the project’s own Prettier version should apply.

The extension README points to a separate troubleshooting guide covering unformatted files, unexpected results, performance and plugin problems. Read it before reinstalling anything.

Autoformat in VSCode FAQ

How do you format every file in a project at once?

The basic editing docs describe only Format Document for the active file and Format Selection for highlighted text. For a whole project, run prettier . –write from the root, which formats all Prettier-supported files in the current directory and its subdirectories.

The flag rewrites files in place, so commit first and list exclusions in a .prettierignore.

Is there a way to save a file without formatting it?

Yes. Run Save without Formatting from the Command Palette. On macOS the command carries the keybinding Cmd+K S.

It skips format on save for that one save, so the setting can stay on everywhere else.

Which setting changes the tab size in VSCode?

editor.tabSize. By default, VS Code inserts spaces and uses 4 spaces per Tab key. The Status Bar indentation display opens a dropdown with indentation commands.

Watch editor.detectIndentation too. Auto-detection checks for indentations of 2, 4, 6 or 8 spaces, so an existing file can win over your global value.

Can Prettier skip projects that have no config file?

Yes. Set prettier.requireConfig to true. It defaults to false, and when true, files need a Prettier configuration file in order to be formatted.

Untitled files still use the VS Code Prettier settings.

Keeping Unformatted Code Out of the Repository

Autoformat in VSCode protects only the editors configured for it, so a pipeline check has to catch what a teammate’s editor missed. Prettier’s prettier –check . does that without changing a file.

It exits with 0 when every file is formatted and 1 when some are not. Exit code 2 means Prettier itself hit a problem. A 1 fails the build.

The order matters, so do these in sequence.

  1. Commit the shared config files.
  2. Reformat the whole project once, in its own commit.
  3. Run the check on every pull request.

The one-time reformat comes before the check, otherwise the first run fails on every legacy file. Once that baseline commit lands, wire the check into a continuous integration pipeline.

The trade-off is a failed merge over spacing, in exchange for no style comments in review. Teams formatting Python with Black or Ruff need the equivalent check from those tools.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.