Everyday Toolkit

Auto Format Code in VS Code: A Complete Setup Guide

Published 2026-07-26

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.

Key takeaways
  • Format code automatically on save using simple keyboard shortcuts or settings
  • Integrate tools like Prettier and ESLint without style conflicts
  • Enforce team-wide code standards with shared repository config files
  • Troubleshoot non-responsive formatters using output logs and language settings
Auto Format Code in VS Code: A Complete Setup Guide
Photo: Dave Dugdale (BY-SA) via Openverse

Auto Format Code in VS Code: A Complete Setup Guide

Auto formatting code in Visual Studio Code requires triggering the built-in Shift + Alt + F shortcut (or Shift + Option + F on macOS) or enabling the "Format On Save" setting to instantly align indentation, brackets, and quotes across your files. Setting up dedicated extensions like Prettier or Black gives you fine-grained control over language-specific rules.

Why Auto Formatting Code Matters

Auto formatting standardizes code structure across your entire repository, removing manually added whitespace, mismatched indentation, and inconsistent quote usage. By automating style rules, development teams prevent endless debates during pull request reviews, maintain readable codebases, and free up mental energy to focus on writing functional software rather than manually adjusting layout grids.

When multiple developers contribute to the same project, everyone brings distinct coding habits. Without automated styling, pull request diffs get cluttered with meaningless changes—like someone swapping double quotes for single quotes or converting spaces to tabs. Automated formatters strip out that noise. You get clean git histories where every line change reflects actual logic updates, not cosmetic tweaks. Over time, this small shift eliminates petty review comments and lets senior devs focus on architecture during code reviews.

VS Code’s Built-in Formatting Capabilities

Auto Format Code in VS Code: A Complete Setup Guide
Photo: “Caveman Chuck” Coker (BY-ND) via Openverse

Visual Studio Code includes native formatting tools capable of managing basic whitespace, line breaks, and indentation out of the box for standard file types. You can trigger formatting manually using Shift + Alt + F on Windows/Linux or Shift + Option + F on macOS, or format specific selections with Ctrl + K, Ctrl + F.

While native formatting works fine for basic HTML, JSON, or quick edits, it lacks advanced, language-aware rules out of the box. For example, it won't automatically reorder imports, enforce trailing commas in complex object literals, or wrap long strings at precise character limits. You can customize basic parameters like tab size and space insertion inside VS Code's global settings, but most production workflows rely on specialized extensions to handle those nuances.

To turn on baseline auto-formatting whenever you save a file, open your settings (Ctrl + , or Cmd + ,), search for "Format On Save," and check the box. You can also add this directly to your settings.json file:

"editor.formatOnSave": true

Specialized extensions significantly extend VS Code's baseline capabilities by applying complex language-specific rules and abstract syntax tree transformations. Popular choices include Prettier for multi-language opinionated formatting, ESLint for JavaScript and TypeScript quality enforcement, Black for strict Python compliance, and C/C++ tools for custom AST configurations.

These tools go way beyond fixing broken indentation. They actively parse your code structure, standardizing line wrappings, array structures, and object destructuring across your whole project. Here is a look at what the main players bring to the table:

  • Prettier: An opinionated, multi-language formatter supporting JavaScript, TypeScript, CSS, HTML, JSON, GraphQL, and Markdown. It re-formats your entire file based on a maximum line length, ignoring original whitespace decisions.
  • Black: Known as "the uncompromising Python code formatter." Black produces small, clean diffs by enforcing a strict uniform layout across Python codebases, offering minimal customization options on purpose.
  • ESLint: Primarily a linter for catching logical bugs, ESLint also includes fixable style rules for JavaScript and TypeScript.
  • EditorConfig: Works alongside formatters by defining top-level editor settings (like character sets and end-of-line characters) across different IDEs using a shared .editorconfig file.

Configuring Prettier for VS Code

Configuring Prettier requires setting it as your default formatter in VS Code settings and defining style preference rules within a project root configuration file like .prettierrc. This setup lets you control specific variables such as line width, tab spacing, trailing commas, and single versus double quotes across your team's development environment.

After installing the Prettier extension from the VS Code Marketplace, set it as your primary document formatter. Open your settings JSON and add these key-value pairs:

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true
}

To enforce project-specific rules that override user preferences, drop a .prettierrc JSON file into your root project directory. Here is an example setup:

{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "printWidth": 80
}

Checking this configuration file into version control guarantees that every contributor formats code identically, regardless of their personal local VS Code settings.

Integrating ESLint and Prettier

Integrating ESLint and Prettier involves using ESLint strictly for catching functional bugs or syntax issues while delegating all pure formatting duties directly to Prettier. Disabling ESLint's stylistic rules using eslint-config-prettier prevents both utilities from fighting over code structure, ensuring smooth execution whenever you trigger automatic formatting.

A frequent headache in web development happens when ESLint and Prettier disagree on formatting rules. ESLint throws a warning about a missing semi-colon, Prettier removes it, and you end up in an infinite loop of auto-fix battles. You can fix this by installing eslint-config-prettier in your project, which turns off all ESLint rules that are unnecessary or might conflict with Prettier.

Update your .eslintrc configuration file so Prettier gets the final say on formatting:

{
  "extends": [
    "eslint:recommended",
    "prettier"
  ]
}

This clear separation keeps your workflow fast: ESLint catches dead variables and structural bugs, while Prettier handles aesthetics.

Comparison of Code Formatting Tools

Selecting the proper formatting tool depends heavily on your primary programming language, project scale, and whether you need pure style formatting or deeper static code analysis. Multi-language ecosystems thrive on Prettier, Python projects rely on Black, and JavaScript teams frequently combine ESLint with Prettier for comprehensive coverage.

Tool Best For License Tier Key Capability
Prettier JavaScript, TypeScript, HTML, CSS, JSON Free (Open Source) Opinionated, automatic code layout rewriting
ESLint JavaScript & TypeScript linting Free (Open Source) Detecting code smells and syntax bugs
Black Python projects Free (Open Source) Strict, non-configurable Python layout standard
EditorConfig Cross-editor project settings Free (Open Source) Enforces line endings, encoding, and basic indent across IDEs

In practice, developers structure their choices based on language ecosystems:

  1. Web Frontend Projects: Use Prettier for layout and styling alongside ESLint for logic checks.
  2. Python Applications: Use Black alongside Flake8 or Ruff for linting.
  3. Multi-IDE Development Teams: Add EditorConfig to the repository to lock in tab sizes and file encoding across VS Code, IntelliJ, and Sublime Text.

Troubleshooting Common Issues

When auto-formatting stops working, the issue usually stems from missing default formatter assignments, conflicting extensions, or syntax errors in the active document. You can resolve most failures by setting a default formatter in settings, inspecting VS Code's Output channel for extension errors, and confirming that "Format On Save" is explicitly toggled on.

If saving a file suddenly stops triggering format updates, walk through these quick diagnostic steps:

  • Check syntax errors: Tools like Prettier and Black refuse to format files containing broken syntax or unclosed tags to prevent corrupting your code.
  • Inspect the Output window: Open the Output panel (Ctrl + Shift + U or Cmd + Shift + U), select your formatter (e.g., Prettier) from the dropdown, and check for runtime errors or missing dependencies.
  • Verify document configuration: Right-click inside your editor, choose "Format Document With...", and select "Configure Default Formatter..." to make sure VS Code knows which extension owns that file extension.
  • Check language-specific overrides: Look inside your settings.json to ensure a language override (like "[python]": { "editor.formatOnSave": false }) isn't silently blocking execution.

Maintaining Clean Codebases Over Time

Maintaining a dependable auto-formatting setup requires committing configuration files directly to source control and keeping extension settings uniform across your team. By combining project-level configurations with git pre-commit hooks, developers can guarantee that unformatted code never reaches production, preserving a clean repository without requiring manual oversight.

To eliminate reliance on individual developer setups, consider adding a pre-commit hook using tools like Husky and lint-staged. These run your chosen formatter over modified files automatically before git accepts a commit.

  • Commit .prettierrc and .vscode/settings.json directly into your repository.
  • Configure git pre-commit hooks to catch unformatted files before they hit remote branches.
  • Use CI/CD pipeline checks (like prettier --check .) to reject pull requests that bypass local formatting rules.

FAQ

How do I format an entire workspace in VS Code?

Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P), type "Format Document", and press Enter. To format multiple files across your workspace simultaneously, you can run a terminal script using Prettier or your language's CLI tool.

Can I disable auto-formatting in VS Code for specific languages?

Yes. Open your settings.json file and add a language-specific block. For example, use "[markdown]": { "editor.formatOnSave": false } to prevent automatic formatting from running when saving Markdown files.

What is the main difference between ESLint and Prettier?

ESLint analyzes code to find code quality issues, potential bugs, and bad practices. Prettier focuses strictly on visual styling, rearranging indentation, line lengths, quotes, and spacing without changing how your code executes.

How do I share my Prettier configuration with my team?

Create a .prettierrc configuration file in the root directory of your project, define your formatting rules in JSON or YAML, and commit it to your version control system so teammates automatically inherit the settings.

Why isn't my code formatting automatically when saving?

Check that "Format On Save" is enabled in VS Code settings and that you have a default formatter configured for the current file type. Also ensure your file contains no syntax errors blocking the parser.

Editorial Team Author & reviewer

Hands-on reviewers testing tools, apps and services so you do not have to. Every article here is hands-on tested and human-reviewed before publishing.