Formatting Code in VS Code: Setup, Shortcuts, and Extensions
Updated Jul 2026
Some links on this page are affiliate links. If you buy through them we may earn a small commission at no extra cost to you. We only recommend what we'd use.
- Built-in shortcuts handle quick cleanup
- Extensions like Prettier offer advanced rules
- settings.json enables workspace-level control
- Format-on-save automates code hygiene

Formatting Code in VS Code: Setup, Shortcuts, and Extensions
Formatting code in VS Code takes just a second using the default keyboard shortcut (Shift+Alt+F on Windows/Linux or Shift+Option+F on macOS), but setting up auto-formatting on save and choosing the right extensions gives you long-term, project-wide consistency.
Getting Started: VS Code's Built-in Formatting
VS Code includes built-in formatting support out of the box for core web technologies like JavaScript, TypeScript, HTML, CSS, and JSON. You can format any active file instantly by pressing Shift+Alt+F on Windows and Linux, or Shift+Option+F on macOS, or by right-clicking inside the editor and selecting "Format Document."
If you only want to tidy up a specific section rather than the entire file, highlight the lines you want to clean up and press Ctrl+K followed by Ctrl+F (Cmd+K then Cmd+F on macOS). This selection shortcut comes in handy when working inside legacy codebases where reformatting an entire 2,000-line file would create messy, unreadable Git diffs for your teammates.
The default engine handles basic indentation, line breaks, and bracket spacing without extra setup. However, it relies strictly on standard language specifications. It won't enforce opinionated style rules like single quotes versus double quotes, trailing commas, or strict line-length caps. For those features, you'll want to tap into the VS Code extension marketplace.
Leveraging Extensions for Advanced Formatting

Popular programming languages such as Python, C++, and PHP require dedicated extensions for custom formatting rules. Installing extensions like Prettier, Black, or Clang-Format expands VS Code's baseline capabilities, unlocking advanced features like opinionated code layouts, deep AST parsing, and seamless integration with existing project linters across large development teams.
Adding a new formatter takes only a few clicks. Open the Extensions view by clicking the sidebar icon or hitting Ctrl+Shift+X (Cmd+Shift+X on macOS), search for your tool, and click Install. Once installed, VS Code usually prompts you to set the new extension as your default formatter the next time you format a document.
Using language-specific extensions stops style debates before they start. Developers generally prefer delegating formatting decisions to a tool that parses the Abstract Syntax Tree (AST) directly, ensuring that code structure stays identical regardless of who authored the changes.
Choosing the Right Formatting Extension
Selecting the right formatting extension depends on your language ecosystem and team preferences. Web developers lean toward Prettier for auto-formatting JavaScript, HTML, and CSS without tedious rule tweaking. Python projects often standardize on Black for uncompromised consistency, while C and C++ codebases rely heavily on clang-format for intricate, rule-based layout adjustments.
Here is how the leading choices stack up across common languages:
| Tool | Primary Languages | Configuration Style | Key Benefit |
|---|---|---|---|
| Prettier | JavaScript, TypeScript, HTML, CSS, JSON | Minimal config (.prettierrc) | Extensive ecosystem and multi-language support |
| Black | Python | Zero/Minimal config (pyproject.toml) | Strict compliance with PEP 8 standards |
| clang-format | C, C++, Java, JavaScript | Highly customizable (.clang-format) | Deep control over bracket styles and indentation |
| EditorConfig | Language-Agnostic | Simple file-based (.editorconfig) | Maintains core rules across different IDEs |
🛍 Ready to buy? Check current prices on Amazon for the picks in this guide.
Prettier remains the standard across front-end web stacks due to its support for React, Vue, and modern CSS modules. For Python developers, Black takes an intentional "uncompromising" stance: it offers few configuration knobs, which eliminates endless discussions over single versus double quotes during PR reviews.
Configuring VS Code Formatting Rules
You can customize VS Code's formatting behavior globally or per workspace using the Graphical Settings editor or the underlying settings.json file. Adding settings like "editor.formatOnSave": true automates cleanup, while specifying "editor.defaultFormatter" ensures that competing extensions do not conflict when processing your code files.
Relying on manual keyboard shortcuts means someone on the team will eventually forget to format before committing. You can automate the whole routine by searching for "Format On Save" in VS Code settings (Ctrl+, or Cmd+,) and checking the box. Alternatively, set "editor.formatOnType": true if you like seeing your code adjust dynamically as you type closing brackets and semicolons.
When multiple formatters exist on your machine, VS Code needs explicit direction. You can set global formatters or scope them down to specific languages using language-specific configuration blocks inside your settings configuration.
Understanding Settings.json
The settings.json file stores all VS Code configurations in plain text JSON format, making it easy to version-control project standards. You open it via the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) by searching for "Open User Settings (JSON)" to directly edit keys, assign formatters, and manage indentation rules.
Here is a typical settings.json snippet that configures format-on-save alongside language-specific default engines:
{
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
Placing a custom .vscode/settings.json file in your project root allows you to commit these preferences right into Git. Every developer who opens the workspace automatically inherits the exact same indent styles and save behaviors, keeping commits uniform across different operating systems.
Integrating Formatting with Version Control
Integrating formatting into your Git workflow guarantees that unformatted code never reaches production or shared code repositories. Using pre-commit hooks via tools like Husky and lint-staged runs your chosen formatter automatically on staged files right before a commit, preventing messy diffs and style-related arguments during pull request reviews.
Even with "Format on Save" turned on locally, team members might edit files outside of VS Code or push code from emergency hotfix branches. Setting up a pre-commit hook guarantees that only clean files get staged. Tools like lint-staged check only the changed files, keeping Git operations fast and smooth.
On top of local hooks, run a formatting check inside your CI/CD pipelines (using commands like prettier --check .). If a pull request contains unformatted code, the continuous integration build fails, giving developers immediate feedback before a human reviewer even opens the pull request.
Troubleshooting Common Formatting Issues
Formatting failures in VS Code usually stem from extension conflicts, missing default formatter declarations, or syntax errors in settings.json. If pressing the format shortcut yields no changes, verify that a default formatter is selected for that file type, ensure no syntax errors block parsing, and check the Editor Output tab.
When VS Code silently ignores your format commands, run through these diagnostic steps to fix the issue quickly:
- Check the Active Output Log: Open the Output panel (Ctrl+Shift+U / Cmd+Shift+U) and switch the dropdown menu from "Tasks" to your formatter (e.g., "Prettier" or "Black"). Syntax errors in your code often cause formatters to quietly abort to prevent breaking your work.
- Verify Language Mode: Look at the bottom-right corner of the status bar. If a
.jsxfile is misidentified as plain "JavaScript" instead of "JavaScript React", the assigned formatter might skip processing it entirely. - Resolve Multiple Formatter Conflicts: Right-click inside your editor and select "Format Document With...". Click "Configure Default Formatter..." from the dropdown to permanently set the primary extension for that language.
Best Practices for Maintaining Clean Code in VS Code
Establishing consistent formatting across individual environments and collaborative teams requires combining VS Code settings with workspace-level configurations. Following a clear set of practices reduces setup friction and keeps repositories clean.
- Commit Workspace Settings: Store project-specific settings in
.vscode/settings.jsonso team members do not have to configure formatters manually. - Use EditorConfig: Include an
.editorconfigfile to enforce fundamental rules like indent style, tab width, and trailing whitespace across various editors outside VS Code. - Automate Cleanups: Turn on format-on-save locally and enforce formatting checks inside your automated CI/CD builds.
- Scope Scans Wisely: Ignore build artifacts, generated bundles, and vendor folders (like
node_modules/or.venv/) using.prettierignorefiles to speed up editor response times.
FAQ
How do I format a specific file in VS Code?
Right-click anywhere inside the file and select "Format Document." You can also press Shift+Alt+F on Windows/Linux or Shift+Option+F on macOS. To format only selected code, highlight the target lines and press Ctrl+K Ctrl+F (Cmd+K Cmd+F on Mac).
Can I format code on save in VS Code?
Yes. Open Settings (Ctrl+, or Cmd+,), search for "Format On Save", and check the box. You can also add "editor.formatOnSave": true directly to your settings.json file.
How do I disable a specific formatter?
Open settings.json and locate the language block you want to change. Remove the "editor.defaultFormatter" line, or set its value to null to stop VS Code from invoking that extension automatically.
What is EditorConfig, and how does it help?
EditorConfig is a file format that defines baseline formatting rules—like indentation styles and end-of-line characters—across different editors. Installing the EditorConfig extension for VS Code reads these files and applies consistent formatting rules automatically.
Where can I find more information about VS Code formatting?
Visit the official VS Code documentation under the "User Guide > Formatting" section. You can also review extension documentation on the official VS Code Marketplace for language-specific flags and features.
🛍 See today's best prices on Amazon and grab the option that fits you.