Auto Format Code in VS Code: Setup, Extensions, and Best Practices
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.
- Enable format-on-save in user settings
- Master manual keyboard shortcuts
- Assign default formatters per language
- Combine ESLint and Prettier seamlessly

Auto Format Code in VS Code: A Complete Guide
Auto-formatting code in Visual Studio Code requires triggering the built-in shortcut (Shift + Alt + F on Windows/Linux or Shift + Option + F on macOS) or enabling the editor.formatOnSave setting in your preferences. Installing dedicated extensions like Prettier, ESLint, or Black allows you to apply strict, language-specific formatting rules automatically.
Why Auto-Format Your Code in VS Code?
Auto-formatting eliminates manual spacing, indentation, and alignment tasks, keeping codebases clean and uniform across entire engineering teams. By automating style enforcement, developers spend less time arguing over code formatting in pull requests and more time reviewing actual application logic and architecture.
Working on a codebase with inconsistent indentation and arbitrary line breaks creates cognitive fatigue. Every developer writes code slightly differently—some prefer two spaces, others four; some favor single quotes, while others use double quotes. Auto-formatting acts as an impartial style authority. It formats messy blocks of code instantly, cleans up git diffs by preventing arbitrary white-space changes, and lets engineers maintain focus on problem-solving rather than manual housekeeping.
Understanding VS Code's Built-in Formatting

VS Code includes native formatting support out of the box for core web standards like HTML, CSS, JavaScript, and JSON without requiring extra plugins. You can format an entire file immediately using the keyboard shortcut Shift + Alt + F (Windows/Linux) or Shift + Option + F (macOS), or format a highlighted selection via the Command Palette.
If you prefer mouse navigation, open the editor menu and select Edit > Format Document. To format only a specific block of code, highlight the lines, right-click, and choose Format Selection (or press Ctrl + K, Ctrl + F). While built-in formatters handle fundamental structural cleanup reliably, complex multi-language projects often require specialized ecosystem extensions to enforce strict, custom rule sets across diverse teams.
Configuring Default Formatting Settings
You configure default formatting settings in VS Code by accessing the Settings menu (Ctrl + , or Cmd + ,) and searching for formatting keys like editor.formatOnSave, editor.defaultFormatter, and editor.tabSize. Adjusting these values globally or within workspace files forces the editor to apply your preferred code style automatically.
While the UI toggle switches work well, setting these values directly in your JSON configuration file provides clearer visibility. You can open your settings.json file via the Command Palette (Ctrl + Shift + P or Cmd + Shift + P) by typing "Preferences: Open User Settings (JSON)". To automate formatting every time you save a file, include these key-value pairs:
{
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.tabSize": 2,
"editor.insertSpaces": true
}
Applying editor.formatOnPaste ensures that code copied from external documentation or stack traces automatically matches your active document's style rules immediately upon pasting.
Popular Code Formatting Extensions
Specialized formatting extensions extend VS Code beyond basic native capabilities by integrating deep language parsers and ecosystem-standard style guides directly into the editor. Popular extensions like Prettier, ESLint, and Black handle complex syntax rules, enforcing project-wide consistency every time you save or commit your code.
Unlike basic regex search-and-replace routines, modern language formatters parse your source code into an Abstract Syntax Tree (AST). They re-print the code entirely from scratch based on structural rules, ignoring original spacing choices. This architecture guarantees that syntax edge cases are handled safely without breaking program execution.
Prettier: The All-Language Formatter
Prettier is an opinionated code formatter supporting JavaScript, TypeScript, CSS, HTML, JSON, GraphQL, and Markdown that automatically reformats code into a standardized layout. It parses code into an abstract syntax tree and re-prints it based on maximum line length, ignoring original spacing and style choices.
To use Prettier effectively in VS Code, install the "Prettier - Code formatter" extension from the Marketplace. Once installed, set Prettier as your default formatter in your settings file to prevent conflicts with native handlers:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Teams generally control Prettier's behavior through a .prettierrc file placed in the repository root. This file establishes configuration options like "singleQuote": true or "trailingComma": "all", ensuring every team member's editor behaves identically regardless of their personal local preferences.
ESLint: For JavaScript & TypeScript
ESLint acts primarily as a static code analysis tool to identify programmatic errors, but it can also fix formatting bugs automatically when configured with formatting rules. Paired with VS Code, ESLint checks syntax against team standards and cleans up messy imports or missing semicolons on save.
While Prettier handles aesthetics (wrapping lines, quotes, tab spacing), ESLint focuses on code quality (detecting unused variables, enforcing variable scope, preventing syntax traps). Combining them requires installing the ESLint extension and configuring your VS Code settings to trigger ESLint fixes during the save action:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
To prevent conflicts where ESLint and Prettier fight over formatting choices, use the eslint-config-prettier plugin in your project. This plugin disables all ESLint rules that clash with Prettier's layout decisions, letting each tool handle what it does best.
Black: Python's Uncompromising Formatter
Black is an uncompromising Python code formatter that enforces a strict, deterministic style with minimal configuration options, ensuring all formatted code looks identical across projects. It automatically restructures Python files to comply with PEP 8 standards, standardizing string quotes and multi-line data structures.
To configure Black in VS Code, install the official Microsoft "Black Formatter" extension from the Marketplace. After installation, explicitly assign Black as the default provider for Python files within your settings configuration:
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}
}
Black limits options deliberately, offering few style flags beyond line length customization. This rigid design philosophy saves development teams from wasting hours debating code styles in technical reviews, as Black's output remains uniform regardless of who wrote the original code.
Comparing Formatting Tools
Choosing between Prettier, ESLint, and Black depends on your project's primary programming languages, code linting requirements, and team tolerance for configuration. Multi-language web stacks benefit from Prettier, JavaScript-heavy applications require ESLint for quality checks, and Python codebases rely on Black for strict PEP 8 compliance.
| Tool | Best For | Primary Language Support | Configuration File |
|---|---|---|---|
| Prettier | Multi-language web projects requiring layout consistency | JavaScript, TypeScript, HTML, CSS, JSON, Markdown | .prettierrc |
| ESLint | Catching logic bugs and enforcing JavaScript/TypeScript code quality | JavaScript, TypeScript, JSX | .eslintrc.json / eslint.config.js |
| Black | Opinionated, automated PEP 8 formatting for Python codebases | Python | pyproject.toml |
🛍 Ready to buy? Check current prices on Amazon for the picks in this guide.
- Prettier: Optimal choice for full-stack web projects that need unified formatting across frontend templates, stylesheets, and scripts.
- ESLint: Essential for JavaScript and TypeScript codebases where static analysis and catchable runtime error detection take priority alongside basic formatting.
- Black: Standard choice for Python services seeking zero-configuration formatting that eliminates code style discussions entirely.
Troubleshooting Common Formatting Issues
When auto-formatting fails in VS Code, the issue usually stems from extension conflicts, missing default formatter declarations, syntax errors in the source code, or misconfigured settings files. Resolving these issues involves inspecting the editor’s Output panel, verifying workspace JSON configurations, and specifying default formatters per language.
If your document refuses to format when saved, run through this diagnostic process:
- Check for Syntax Errors: AST-based formatters will refuse to process files containing active syntax errors to avoid corrupting your logic. Check the "Problems" tab (
Ctrl + Shift + M) first. - Inspect Formatter Output: Open the Output panel (
Ctrl + Shift + U) and select your active formatter (e.g., "Prettier") from the dropdown list on the right. Look for parsing failures or missing configuration file errors. - Resolve Multiple Formatter Prompt: If you have multiple extensions installed that target the same language, right-click inside the active file, choose Format Document With..., and pick Configure Default Formatter... to lock in your choice.
- Verify Extension Status: Confirm that the required extension is active globally and not disabled within your current workspace.
Advanced Configuration and Customization
Advanced formatting workflows in VS Code utilize language-specific configuration overrides, git hook automation via tools like Husky and lint-staged, and continuous integration checks. These practices guarantee that unformatted code cannot be saved, committed, or merged into production branches regardless of an individual developer's local editor settings.
For projects involving distinct technologies, define settings per language directly inside your settings.json file. This prevents generic formatting rules from breaking language-specific idioms:
{
"editor.formatOnSave": true,
"[markdown]": {
"editor.formatOnSave": false,
"editor.wordWrap": "on"
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
To safeguard repositories against unformatted code uploaded by team members who don't use VS Code, set up pre-commit hooks using husky and lint-staged. These utilities automatically run formatters across modified files staged for git commits, rejecting unformatted code before it enters your commit history.
Best Practices for Team Alignment
Aligning code formatting across development teams requires checking configuration files into version control and sharing workspace-level settings across the organization. Standardizing settings inside repository root files eliminates local developer discrepancies, guarantees uniform formatting during pull request reviews, and speeds up continuous integration pipelines.
- Check in Workspace Settings: Create a
.FAQ
How do I auto format code in VS Code?
Use the keyboard shortcut Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS). You can also find the "Format Document" command in the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
What extensions can help with auto formatting?
Popular extensions include Prettier, ESLint, and others specific to your language. These often offer more advanced formatting rules and linting capabilities.
Why isn't auto formatting working?
Check your VS Code settings to ensure formatting is enabled. Verify your language extension is installed and configured correctly, and that there are no conflicting configurations.
🛍 See today's best prices on Amazon and grab the option that fits you.
More from us: Kiruruchiki — more tips & how-to guides