VS Code Formatter: Shortcuts, JSON, Python, Format on Save

November 11, 2026 · 10 min read

VS Code Formatter: Format Document, Shortcuts and Format on Save

The VS Code formatter runs when you trigger Format Document: Shift+Alt+F on Windows, Shift+Option+F on macOS, or Ctrl+Shift+I on Linux. VS Code formats JavaScript, TypeScript, JSON, HTML and CSS out of the box. For Python and most other languages, you install a formatter extension, make it the default, and turn on format on save.

What Is the Keyboard Shortcut for Formatting Code in VS Code?

VS Code has two formatting commands. Format Document formats the whole file, and Format Selection formats only the highlighted text. The shortcut depends on your operating system, which is why so many answers online look contradictory.

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

If you forget the VS Code formatter shortcut, open the Command Palette and type "Format Document", or right-click inside the editor and pick it from the context menu. Format Selection with Ctrl+K Ctrl+F is a two-step chord: press Ctrl+K, release, then press Ctrl+F.

You can rebind either command. Run Preferences: Open Keyboard Shortcuts from the Command Palette, or use File > Preferences > Keyboard Shortcuts. Search for the command ID, editor.action.formatDocument or editor.action.formatSelection, and double-click the row to record a new key combination. For Markdown editing keys such as bold, links and preview toggles, see our list of Markdown shortcuts.

Which Formatter Does VS Code Use for Each Language?

Every file has a language mode, shown in the Status Bar at the bottom right. When you press Format Document, VS Code looks for a formatter registered for that language. Per the VS Code formatting docs, default formatters ship for JavaScript, TypeScript, JSON, HTML and CSS. Everything else needs an extension. So the formatter in VS Code isn't one program: it's a slot that a built-in language feature or an installed extension fills, one language at a time.

Three situations cover almost every case:

  • One formatter is registered. It runs, and you never see a prompt.
  • None is registered. VS Code shows "There is no formatter for 'python' files installed." with an Install Formatter button that searches the Marketplace.
  • Two or more are registered and none is the default. VS Code reports that there are multiple formatters and asks you to configure one as the default.

The fix for the third case lives in the context menu. Right-click, choose Format Document With..., then Configure Default Formatter..., and pick the extension. VS Code saves that choice for the current language only. To browse what's available, type category:formatters in the Extensions view search box.

Setting a Default Formatter Per Language in settings.json

Settings are where you tell the Visual Studio Code formatter which extension wins. The picker writes an editor.defaultFormatter entry for you, but you can write it by hand, which is easier to review and share. Install the formatter extensions first, then run Preferences: Open User Settings (JSON) from the Command Palette and add language blocks like these:

{
  "[javascript][typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[python]": {
    "editor.defaultFormatter": "charliermarsh.ruff"
  }
}

The value is always an extension ID, the publisher.name string shown on the extension's Marketplace page. A bracketed block such as [python] applies only to that language, and the settings docs say language-specific values beat general ones. An editor.defaultFormatter line outside any brackets covers every other language, so add one only if that formatter handles most of your files. For a language it can't format, VS Code reports that the extension is configured but cannot format those files.

User settings follow you everywhere. Workspace settings live in .vscode/settings.json at the project root, override your user settings, and get committed with the code, so teammates who open the folder get the same formatter choices. Put language blocks there too, because a user-level [python] block beats a workspace-level top-level setting. Markdown files follow the same pattern, and our Markdown formatter guide has the exact block for them.

Using the VS Code JSON Formatter

You don't need an extension for JSON. The Visual Studio Code JSON formatter is part of the built-in JSON language features, so Format Document works on any .json file with no setup. Paste a minified response and format it:

{"name":"demo","version":"1.0.0","scripts":{"build":"tsc","test":"vitest"},"private":true}

Format Document expands it to one property per line. The indent width comes from editor.tabSize, which defaults to 4; this output uses 2:

{
  "name": "demo",
  "version": "1.0.0",
  "scripts": {
    "build": "tsc",
    "test": "vitest"
  },
  "private": true
}

Two settings control it. json.format.enable (on by default) turns the built-in formatter on or off, and json.format.keepLines (off by default) keeps all existing line breaks when formatting. VS Code's own settings.json, tasks.json and launch.json open in JSON with Comments mode, whose language ID is jsonc. The JSON docs explain that this mode accepts comments, and the formatter leaves them in place. To style both modes at once:

{
  "[json][jsonc]": {
    "editor.defaultFormatter": "vscode.json-language-features",
    "editor.tabSize": 2,
    "editor.formatOnSave": true
  }
}

The built-in extension also adds a JSON: Sort Document command that sorts object keys. Switch the VS Code JSON formatter to Prettier only when your project already has a Prettier config, so JSON follows the same rules as everything else.

Choosing a VS Code Python Formatter: Black, autopep8 or Ruff

Python isn't on the built-in list, so a fresh install has no VS Code Python formatter at all. The Python formatting docs list these extensions:

FormatterExtension IDPublished byStyle
Black Formatterms-python.black-formatterMicrosoftOpinionated, few options
autopep8ms-python.autopep8MicrosoftFixes PEP 8 violations
Ruffcharliermarsh.ruffCommunityBlack-compatible, also sorts imports
yapfeeyore.yapfCommunityHighly configurable

Install one, then point the [python] scope at it. This block uses Black with a 100-character line length:

{
  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true
  },
  "black-formatter.args": ["--line-length", "100"]
}

Each argument goes in as a separate string. The Python docs still warn that Format Selection fails with Black and suggest turning off format on paste. That advice predates Black 23.11, which added a --line-ranges option. The Black Formatter extension passes it to Black 23.11 or later, so selections format with a current Black.

We prefer Ruff for new projects, because one extension handles formatting and import sorting. Add the import step as a code action:

{
  "[python]": {
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports.ruff": "explicit"
    }
  }
}

Turning On Format on Save, Paste and Type

All three automatic triggers are off by default. Pressing a shortcut every time gets old, so it's common to let the VS Code formatter run on save at least:

{
  "editor.formatOnSave": true,
  "editor.formatOnSaveMode": "modificationsIfAvailable",
  "editor.formatOnPaste": true,
  "editor.formatOnType": false
}

editor.formatOnSaveMode decides how much gets formatted. file, the default, formats the whole file. modifications formats only the lines you changed, which requires source control and a formatter that supports Format Selection. modificationsIfAvailable tries that first and falls back to the whole file when there's no source control. It's the polite choice in an old codebase, since your commits won't reformat lines you never touched.

Watch out for auto save. If files.autoSave is set to afterDelay, VS Code only formats when you save explicitly with Ctrl+S (Cmd+S on macOS). Format on paste has a similar catch: it only works with formatters that can format a range of text. To move formatted code out of VS Code, see our guides on making a code block in Word or a code screenshot.

Why the VS Code Formatter Does Nothing (and How to Fix It)

When Shift+Alt+F appears to do nothing, check these causes in order:

  1. No formatter for the language. Look for the "There is no formatter" message and install one.
  2. Wrong language mode. A file detected as Plain Text has no formatter. Click the mode in the Status Bar and pick the right language.
  3. A stale default. If editor.defaultFormatter names an extension you've uninstalled or disabled, VS Code says it's configured but not available. If the extension can't handle this language, it says so instead. Pick a new one with Configure Default Formatter.
  4. A syntax error. Most formatters refuse to touch code they can't parse. Fix the red squiggle first.
  5. The formatter itself failed. Open the Output panel (View > Output) and pick the formatter's channel, such as Black Formatter or Prettier, from the dropdown to read its error.
  6. The file is huge. VS Code skips very large files and warns that the file cannot be formatted because it is too large.

One limitation is worth knowing before you debug further. The built-in formatters expose only a handful of options, such as indent size and json.format.keepLines. If your team needs strict, shared style rules, use a formatter that reads a config file from the repo, such as Prettier or Black.

Formatting Markdown Files in VS Code

Markdown isn't on VS Code's list of built-in formatters, so Format Document reports that no formatter is installed until you add an extension. Our Markdown formatter guide, linked in the settings section, walks through that setup. For preview and Markdown extensions, see our VS Code Markdown guide, and for rule checks a formatter skips, our Markdown lint guide.

Outside VS Code, our online Markdown formatter runs in your browser. It adds a blank line before headings, switches bullets to -, renumbers ordered items to 1., collapses repeated blank lines and trims trailing whitespace. In the editor, the Format Markdown toolbar button is a server-side action that counts toward the free pool of 3 a day.

Once the VS Code formatter knows which extension owns each language, formatting becomes one keystroke or a save. Set a default per language, commit a .vscode/settings.json for your team, and turn on format on save with the mode that suits your repo. For Markdown that lives outside a project, paste it into the editor and keep writing with a live preview beside it.