VS Code

How to Format Code in VSCode Like a Pro

How to Format Code in VSCode Like a Pro

On Windows, Shift+Alt+F formats the file you have open. Mac users press Shift+Option+F, and on Linux it’s Ctrl+Shift+I. The active formatter then rewrites the whole file.

Microsoft’s VS Code documentation (updated 11/18/2026) lists built-in formatters for only 5 languages. Those are JavaScript, TypeScript, JSON, HTML and CSS. Format on save, on paste and on type are all off in a fresh install.

For any other language you install a formatter extension. Some language extensions also change the defaults when you add them. The Go extension, for example, turns format on save on by default.

Which Shortcuts Format Code in VS Code?

YouTube player

Format Document is the one you’ll use most. It’s Shift+Alt+F on Windows, Shift+Option+F on macOS and Ctrl+Shift+I on Linux, and it covers the entire active file.

Format Selection and the Command Palette sit on different keys. Here they are as of the documentation’s 11/18/2026 update.

CommandWindowsmacOSLinux
Format DocumentShift+Alt+FShift+Option+FCtrl+Shift+I
Format SelectionCtrl+K Ctrl+FCmd+K Cmd+FCtrl+K Ctrl+F
Command PaletteCtrl+Shift+PCmd+Shift+PCtrl+Shift+P

Format Selection only touches the lines you highlighted, which is handy when you’re editing someone else’s file and don’t want a giant diff. Both commands also show up in the Command Palette and the right-click menu. If keybindings never stick in your head (they don’t in mine, half the time), that’s the easier route.

You don’t need to change any setting to format manually. A file in one of the built-in languages formats on the first try. If no formatter is installed for the language, VS Code shows a prompt offering to install one.

Since format on save, on paste and on type all start switched off, a new install only formats when you ask it to.

Choosing a Formatter for Each Language

JavaScript, TypeScript, JSON, HTML and CSS work without installing anything.

Everything else comes from the Marketplace. Formatters are ordinary VS Code extensions. Type category:formatters into the Extensions view search and you’ll get the Formatters category.

LanguageFormatterWhere it comes from
Web languages, Markdown, YAMLPrettier (esbenp.prettier-vscode)Marketplace extension
JavaScript, TypeScript, JSON, CSSBiome (biomejs.biome)Marketplace extension
PythonBlack Formatter (ms-python.black-formatter)Marketplace, published by Microsoft
PythonRuff (charliermarsh.ruff)Marketplace, community-offered
C and C++clang-formatDefault in the C/C++ extension
GogoplsGo extension (golang.go)
RustrustfmtRust toolset

Prettier or Biome for web languages

Prettier covers the most ground. Its list includes JavaScript, TypeScript, JSX, JSON, CSS, SCSS, Less, HTML, Vue, Angular, GraphQL, Markdown and YAML, and plugins push it further.

A Prettier setup in VS Code takes one extension. That extension checks for a Prettier config file first, then .editorconfig, and only after that your VS Code settings.

You can install Prettier by running ext install esbenp.prettier-vscode from Quick Open. Biome’s extension ID is biomejs.biome. If you’re on VSCodium or Cursor, you get Biome from the Open VSX registry instead.

My advice is boring but it works: use whatever the repo already runs. The real problem is two formatters fighting over the same file, and you’ll spend an afternoon wondering why your quotes keep flipping.

Black or Ruff for Python

According to Ruff’s documentation, more than 99.9% of lines come out identical to Black on Django and Zulip.

Close isn’t the same as equal, though. Ruff departs from Black on purpose in a few places. It treats line length as best effort, and it doesn’t sort imports.

  • Microsoft publishes Black Formatter as ms-python.black-formatter.
  • Ruff is community-offered. Its docs recommend extension version 2024.32.0 or later.
  • If your settings.json still has old python.formatting entries, remove them. They’re no longer supported (Python docs, page dated 02/04/2026).

Install whatever CI runs. A Black pipeline means Black in the editor, and a Ruff pipeline means Ruff.

C, C++, Go and Rust

The C/C++ extension uses clang-format by default and takes its rules from a .clang-format file in the workspace. Without that file, the fallback style setting kicks in, and it defaults to Visual Studio.

Go formatting comes from gopls. Remember that the Go extension turns format on save on by default, so you may not have to do anything.

Rust ships rustfmt as part of the Rust toolset.

How to Set a Default Formatter, Globally or Per Language

In settings.json, set editor.defaultFormatter to the formatter’s extension ID. You can put it at the top level or inside a language block like [python].

  1. Open a file in the target language and run Format Document With… from the Command Palette.
  2. Choose Configure Default Formatter, then pick the formatter from the list.
  3. To do it by hand, set Editor: Default Formatter to the extension ID, for example esbenp.prettier-vscode.
  4. For one language only, add a language block that names a different formatter.
  5. Press the Format Document shortcut again to confirm the right formatter runs.

Once you know the IDs, it’s faster to go straight to the Open Settings (JSON) command. You can follow the steps in this guide to opening settings.json.

{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}

Use a separate block for each language. Prettier’s README says a combined key such as [javascript][typescript] does not work for editor.defaultFormatter.

Overrides go in either direction. If you want Prettier on everything except JavaScript, set it globally and give [javascript] a different formatter. The reverse also works: set some other formatter globally, then name Prettier only inside [javascript].

Don’t leave the default unset. In October 2020, Prettier users found format on save had stopped working after a VS Code update, and it turned out the default formatter was null (microsoft/vscode issue 108447).

When more than one formatter can handle a file, VS Code asks you to pick. That’s fine on your own machine. In a shared workspace, write the setting down explicitly.

Turning On Format on Save, Paste, and Type

The settings are editor.formatOnSave, editor.formatOnPaste and editor.formatOnType, and they all default to off.

Format on save

This is the one most people want. It’s a single boolean, and the Prettier extension respects it.

{
"editor.formatOnSave": true
}

You can scope it per language by setting it to false globally and true inside [javascript]. Going the other way works too. The Go docs show editor.formatOnSave set to false under [go] to turn it off for that one language.

The setting’s own description adds two conditions. A formatter has to be available, and the file can’t be saved after a delay.

If a linter also fixes code, pair this with editor.codeActionsOnSave. One catch from Prettier’s README: the source.fixAll.prettier action respects editor.defaultFormatter. So if a different formatter is your default, the action only runs when you set it to “explicit” or “always”.

Format on paste and on type

These need a formatter that can handle part of a file. VS Code’s docs say not all formatters support format on paste, because they must support formatting a selection or range.

On paste, the pasted text gets reformatted to match what’s around it. Format on type depends on the formatter. With clang-format, typing the semicolon is what triggers it.

Black is where this breaks down. The Python docs say Black does not support formatting sections of code. Their suggestion is to turn off format on paste and keep format on save in file mode.

Formatting only modified lines

VS Code 1.49 (August 2020) added editor.formatOnSaveMode, which can limit format on save to the lines you changed. It accepts file, modifications and modificationsIfAvailable. The default is file.

  • file formats the whole file
  • modifications needs source control and won’t format anything if none is active
  • modificationsIfAvailable formats the whole file when there’s no source control

This exists for a good reason. In some codebases, formatting entire files would make diffs against old commits hard to read.

Support varies by formatter, though. The ruff-vscode tracker has an issue saying the modifications modes fail silently and only file works (issue 319). A separate VS Code issue traced a Prettier error to the two modifications modes, not to file.

So test before you trust it. Change one line, save, and look at the diff.

Which Config File Controls Tab Size, Print Width, and Quotes?

It depends on the formatter, since each one reads its own file. For Prettier that’s .prettierrc. Black reads the [tool.black] table in pyproject.toml.

Prettier’s printWidth defaults to 80 columns and tabWidth to 2. Its trailingComma has been “all” since v3.0.0 (it was “es5” before), and endOfLine has been “lf” since v2.0.0, previously “auto”. Black’s line length is 88.

Those numbers don’t line up. Run Prettier on JavaScript and Black on Python in the same repo, and you’ll wrap at 80 columns in one language and 88 in the other until somebody sets both.

Prettier picks up four .editorconfig properties and maps them to its own options: max_line_length to printWidth, indent_size or tab_width to tabWidth, indent_style to useTabs, and end_of_line to endOfLine. The docs list no EditorConfig property for semi, singleQuote or trailingComma. Quote style and trailing commas have to go in .prettierrc.

Inside the editor, the EditorConfig extension (editorconfig.editorconfig) applies indent_style, indent_size and tab_width. On save it also handles end_of_line, insert_final_newline and trim_trailing_whitespace.

The two formatters also search for config differently. Prettier starts in the formatted file’s folder and walks up the file tree until it finds a config file (or doesn’t), and it has no global config at all.

Black stops at whichever comes first: a pyproject.toml holding [tool.black], a .git or .hg directory, or the filesystem root. Command-line options beat pyproject.toml, which beats Black’s defaults.

Two Prettier extension settings change the lookup. prettier.requireConfig is off by default, and when on, it blocks formatting unless a Prettier config file exists. prettier.configPath forces one specific file and ignores local configs.

Your VS Code settings stack by scope as well. Workspace settings override user settings. Language-specific settings win over non-language settings, even ones with a narrower scope.

There’s one syntax detail where the docs seem to disagree. VS Code’s settings docs (dated 11/18/2026) say several languages can share one block written as [javascript][typescript]. The Prettier extension’s README says combined keys do not work for editor.defaultFormatter.

They’re talking about different things. Multi-language blocks work for general editor settings, and the README is specifically excluding the default formatter setting. Writing one block per language for editor.defaultFormatter works under both, so just do that.

Sharing Formatting Rules With a Team and Stopping Prettier-ESLint Clashes

Commit your config files along with the .vscode folder. Then everyone who opens the repo gets the same formatter and the same rules, and you can turn to keeping the formatter and linter from stepping on each other.

Shared project config

Workspace settings live in .vscode/settings.json at the project root. Next to it, .vscode/extensions.json holds a recommendations array of publisher.extension IDs, such as dbaeumer.vscode-eslint and esbenp.prettier-vscode.

Then there’s the formatter’s own config. DigitalOcean’s Prettier guide (updated August 2025) lists .prettierrc and .prettierignore alongside the workspace settings file.

These get shared through version control like any other file. The first time someone opens the workspace, VS Code prompts them to install the recommended extensions.

You don’t have to write extensions.json by hand. The Extensions: Configure Recommended Extensions (Workspace Folder) command creates it. And because Prettier has no global configuration, the project behaves the same on a new machine.

Formatter and linter together

A formatter changes layout. A linter checks code quality.

The trouble starts where they overlap. Linting in programming often includes stylistic rules, and Prettier’s docs say some of those conflict with its output. Give each tool one job and the conflicts go away.

The recommended route is eslint-config-prettier, a pre-made config that disables ESLint rules that conflict with or duplicate Prettier.

Prettier’s docs advise against eslint-plugin-prettier, which runs Prettier as an ESLint rule. You get lots of red squiggles, it’s slower than running Prettier directly, and it adds a layer of indirection where things can break. I’ve never seen a good reason to pick it.

Let Prettier handle formatting and ESLint handle linting, with eslint-config-prettier in between.

When VS Code Formatting Does Not Work

Most failures trace back to one of the documented conditions below.

SymptomCauseFix
Format Selection does nothing in PrettierFile type is outside JavaScript, JSX, TypeScript, TSX, JSON, GraphQL and HandlebarsRun Format Document
Format on save skipped after a pauseAuto Save afterDelay saves are not formattedSave with Ctrl+S
Save hangs on a “Running Save Participants” notificationA slow formatter is still running; since VS Code 1.42 there is no save timeout, and cancelling the notification skips the formatterWait for it to finish, or fix or replace the slow formatter
Saved file stays unformattedVS Code sees no changes, so the formatter never runsMake an edit, then save
Prettier ignores plugins and .editorconfigUntrusted workspace: bundled Prettier, useEditorConfig forced offTrust the workspace
No formatter runs in Restricted ModeExtensions that have not opted into Workspace Trust are disabledTrust the workspace
Prettier does nothing on one projectprettier.requireConfig is on and no config file existsAdd .prettierrc or turn the setting off

Slow formatters are the sneaky one. You’ll still find older guides telling you to raise editor.formatOnSaveTimeout. That 750 ms timeout only applied to VS Code versions before 1.42 (January 2020). Current versions have no timeout and show a progress notification where you can cancel save participants.

Auto Save is easy to misread. When files.autoSave is afterDelay, explicit saves still format, per microsoft/vscode issue 230051 on the setting’s wording.

If format on save seems unreliable, start with the default formatter. After that, look for a stuck “Running Save Participants” notification, and then check the Auto Save mode.

FAQ About Code Formatting in VS Code

How do I turn off auto format in VS Code?

Set editor.formatOnSave to false. If you turned on editor.formatOnPaste or editor.formatOnType, set those to false too.

Still formatting? Then a workspace or language-specific block is overriding your user setting. Check blocks like [go].

Can I change the keyboard shortcut for formatting?

Yes. In the Keyboard Shortcuts editor, rebind editor.action.formatDocument and editor.action.formatSelection. Those are the two command IDs the Prettier extension’s documentation names.

What is the difference between auto indent and formatting?

Scope, mostly. The Editor: Auto Indent setting (with its Full option) only adjusts indentation while you type. A formatter rewrites the layout of a whole file or a selection.

Auto indent makes typing more comfortable. For consistent output across a project you still want a formatter extension.

Does formatting change what my code does?

No. Prettier’s first requirement is output with the exact same behavior as before formatting. It only prints code.

In its default safe mode, Black compares the abstract syntax tree before and after. It has three documented exceptions (docstring whitespace, del parentheses, moved comments), and none of them change runtime behavior.

Formatting an Existing Codebase Without Breaking Git Blame

If you’re starting code formatting in VS Code on a codebase that has never been formatted, don’t flip on format on save first. Do one dedicated reformatting commit. Otherwise style changes leak into every file a contributor happens to touch, and blame gets messy fast.

  1. Reformat the whole project in a single commit.
  2. Put that commit’s full 40-character hash in .git-blame-ignore-revs at the project root.
  3. Turn on format on save after that commit lands.

The file works with git blame –ignore-revs-file, and git config blame.ignoreRevsFile makes it the local default. GitHub and GitLab 17.10 and later honor it, but some other Git hosts do not (Black documentation, verified November 2026).

You do end up with one huge diff that nobody can review line by line. Have reviewers look at the formatter config instead of the output.

For why lines from an ignored revision fall back to the previous commit that changed them, see this closer look at how git blame attributes each line to a commit.

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.