VS Code Formatting Command: Quick Shortcuts and Setup Guide
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.
- Press Shift + Alt + F (Option + Shift + F on Mac) to format any active document immediately
- Set a default formatter in VS Code settings to prevent extension conflicts
- Enable Format on Save to automatically clean up files every time you save
- Use .editorconfig files to enforce team-wide style rules across different editors

VS Code Formatting Command: Quick Shortcuts and Setup Guide
The primary command to format an entire file in Visual Studio Code is Format Document, triggered by pressing Shift + Alt + F on Windows/Linux or Option + Shift + F on macOS.
The Core Command: `Document Formatting`
The primary command to format an entire file in Visual Studio Code is Format Document, accessed via Shift + Alt + F on Windows and Linux or Option + Shift + F on macOS. This command instantly cleans up your code using your active language formatter, organizing indentation, line breaks, and bracket placements according to your configured rules.
If keyboard shortcuts aren't your preference, you can trigger the same action by opening the Command Palette (Ctrl + Shift + P or Cmd + Shift + P), typing "Format Document", and pressing Enter. You can also right-click anywhere inside the editor window and select Format Document from the context menu.
When working on large files where you only want to tidy up a specific block of code, highlight the target text and run Format Selection (Ctrl + K, Ctrl + F or Cmd + K, Cmd + F). This applies your formatting rules strictly to the selected lines without modifying the rest of your file.
Understanding Formatters and Extensions

VS Code relies on specific formatting engines—either built-in language services or third-party extensions—to parse and clean up code. Tools like Prettier, ESLint, and Black act as underlying drivers. Without a compatible formatter installed and set as default, running the format command prompts you to choose one from the Visual Studio Marketplace.
Out of the box, VS Code includes basic formatting support for web technologies like HTML, CSS, and JSON, as well as TypeScript and JavaScript. However, most developers quickly add specialized extensions tailored to their specific languages and framework choices.
Installing an extension adds language-aware intelligence to the editor. For instance, installing the Prettier extension provides opinionated formatting for JavaScript, TypeScript, and JSX, while the Python extension brings tools like Black or Autopep8 into your workspace. Selecting the right engine ensures your code follows standard community conventions without manual intervention.
Configuring Your Default Formatter
Set your default formatter by opening Settings (Ctrl + , or Cmd + ,), searching for default formatter, and choosing your installed tool from the dropdown menu. Alternatively, define "editor.defaultFormatter" directly within your global or workspace settings.json file to enforce specific formatters for individual programming languages across your projects.
Defining a explicit default formatter prevents VS Code from repeatedly asking which tool to use when multiple extensions support the same file extension. You can assign different formatters to different file types inside your settings.json file:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"[cpp]": {
"editor.defaultFormatter": "ms-vscode.cpptools"
}
}
Another popular option is setting "editor.formatOnSave": true. This automatically formats your code every time you save a file, taking the friction out of maintaining clean style guidelines.
Common Formatting Commands and Keyboard Shortcuts
Key shortcuts include Shift + Alt + F for full documents and Ctrl + K, Ctrl + F (Cmd + K, Cmd + F on Mac) to format highlighted selections. Essential commands like Format Document With... and Organize Imports (Shift + Alt + O) give you precise control over tool selection and module imports.
Here is a quick reference for the standard formatting shortcuts available in VS Code across operating systems:
- Format Document:
Shift + Alt + F(Windows/Linux),Option + Shift + F(macOS) - Format Selection:
Ctrl + K, Ctrl + F(Windows/Linux),Cmd + K, Cmd + F(macOS) - Organize Imports:
Shift + Alt + O(Windows/Linux),Option + Shift + O(macOS)
The Format Document With... command is particularly handy when you want to bypass your default formatter temporarily. For example, if you normally format JavaScript with Prettier but want to run ESLint's auto-fix engine on a specific legacy file, this command lets you select a non-default formatter on the fly.
Troubleshooting Common Formatting Issues
When formatting fails, the issue usually stems from conflicting extensions, syntax errors in the file, or missing default formatter declarations. Fix this by explicitly setting a default formatter, reviewing the VS Code Output panel for extension logs, and confirming that your code compiles without active syntax errors.
If pressing the format shortcut results in nothing happening, check the bottom status bar or open the Output panel (Ctrl + Shift + U / Cmd + Shift + U). Select your formatter (such as "Prettier" or "Python") from the drop-down menu on the right side of the Output panel. Any syntax errors that prevent the engine from parsing your file into an Abstract Syntax Tree (AST) will show up clearly here.
Another frequent culprit is conflicting settings between linters and formatters. If ESLint and Prettier fight over single vs. double quotes, ensure you have configured them to delegate formatting responsibilities properly using helper configs like eslint-config-prettier.
Customizing Formatting with EditorConfig
An .editorconfig file in your project root establishes baseline styling rules—such as indentation style, tab width, and trailing whitespace—across different code editors and team members. VS Code respects these rules when the EditorConfig extension is installed, seamlessly overriding global user preferences on a per-project basis.
Using an EditorConfig file ensures that developers working on Linux, macOS, or Windows apply identical indentations and file endings without manually tweaking their personal editor settings. A typical .editorconfig file looks like this:
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Because VS Code reads this file directly, anyone joining your project inherits the exact same basic layout rules automatically upon opening the repository.
Comparison of Popular Formatting Extensions
Choosing the right formatter depends on your tech stack, strictness requirements, and team conventions. Prettier excels at web technologies, ESLint handles JavaScript linting alongside formatting, Black enforces strict Python standards, and Clang-Format standardizes C-based languages. All major formatting extensions are open-source and free to install.
The following table breaks down the leading formatting tools commonly used within Visual Studio Code:
| Tool | Best For | Pricing Tier | Standout Feature |
|---|---|---|---|
| Prettier | JS, TS, HTML, CSS, JSON, Markdown | Free (Open Source) | Opinionated formatting with minimal setup required. |
| ESLint | JavaScript, JSX, TypeScript | Free (Open Source) | Combines code quality linting with customizable style rules. |
| Black | Python | Free (Open Source) | Uncompromising, deterministic layout rules tailored to PEP 8. |
| Clang-Format | C, C++, Java, JavaScript, Protobuf | Free (Open Source) | Granular control over bracket styles and pointer alignments. |
🛍 Ready to buy? Check current prices on Amazon for the picks in this guide.
- Prettier: The most popular web development formatter. It handles mixed-language files like React JSX or Vue single-file components gracefully.
- ESLint: Primarily a linter, but capable of fixing structural and stylistic issues automatically when configured with
--fixrules. - Black: Known as "the uncompromising Python code formatter." It limits style choices so teams spend zero time discussing line lengths or quotes during reviews.
- Clang-Format: Highly configurable utility backed by LLVM, perfect for system developers requiring specific coding standards across C and C++ codebases.
Advanced Formatting Techniques and Workflows
Advanced workflows integrate code formatting directly into git hooks or build pipelines using tools like Husky and lint-staged. This guarantees that unformatted code never enters version control. You can also configure language-specific formatting overrides inside VS Code's settings.json file for precise control over distinct file types.
Automating formatting through pre-commit hooks eliminates human error completely. By setting up lint-staged alongside husky in a Node.js project, Git automatically formats modified files right before a commit is created:
// package.json snippet
"lint-staged": {
"*.{js,css,md}": "prettier --write"
}
This workflow guarantees that dirty or badly indented code cannot make it into pull requests, saving time for reviewers and avoiding unnecessary build failures in continuous integration pipelines.
Best Practices for Team Consistency
Maintaining team-wide code consistency requires committing shared configuration files—such as .prettierrc or .editorconfig—alongside workspace settings in your repository. Automating format checks during continuous integration ensures every pull request adheres strictly to agreed-upon style guidelines without requiring manual code review comments regarding minor formatting issues.
Bikeshedding over double versus single quotes or tab widths is a waste of engineering time. The most effective development teams agree on a configuration file early, check it into git, and enforce it automatically through VS Code's workspace settings.
To lock down editor behavior for everyone on a project, create a .vscode/settings.json file in the repository root containing shared defaults like "editor.formatOnSave": true. When team members open the project in VS Code, the workspace settings will apply automatically without altering their global personal preferences for other work.
FAQ
How do I install a formatting extension in VS Code?
Open the Extensions view by pressing Ctrl + Shift + X or Cmd + Shift + X, type the name of your desired formatter (such as "Prettier"), and click "Install". Once installed, set it as your default formatter in Settings.
Why isn’t my code formatting when I run the command?
This usually happens due to syntax errors in your code, an unassigned default formatter, or conflicting extensions. Check the Output panel (Ctrl + Shift + U) and select your formatter from the dropdown menu to view specific error messages.
Can I format only a highlighted section of code?
Yes, select the code block you want to clean up and press Ctrl + K, Ctrl + F on Windows/Linux or Cmd + K, Cmd + F on macOS. You can also right-click the selection and choose "Format Selection".
How do I enable auto-formatting every time I save?
Open Settings with Ctrl + , or Cmd + ,, search for "Format On Save", and check the box. Alternatively, add "editor.formatOnSave": true to your global or workspace settings.json file.
How do I change indentation settings like tab size?
Click "Spaces" or "Tab Size" in the bottom right corner of the status bar to adjust settings for the active file. To change it globally, search for "Tab Size" in Settings or define 🛍 See today's best prices on Amazon and grab the option that fits you."editor.tabSize":