Format on Save in VS Code
You'll learn how to make VS Code automatically format your code every time you hit save—no more manual cleanup commands.
What Is Format on Save?
Format on Save is a VS Code setting that runs your formatter (like Prettier) automatically whenever you save a file. Instead of manually running a format command or relying on pre-commit hooks, your code gets cleaned up instantly as you work. Think of it like autocorrect for writing—you type naturally, and the editor polishes things behind the scenes.
Enabling Format on Save
Open VS Code settings (File → Preferences → Settings or Cmd/Ctrl + ,) and search for "format on save". Check the box for Editor: Format On Save.
Alternatively, add this to your settings.json:
{
"editor.formatOnSave": true
}
Setting a Default Formatter
If you have multiple formatters installed, tell VS Code which one to use. For Prettier across all files:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
Language-Specific Formatters
You can configure different formatters per language. For example, use Prettier for JavaScript but a different tool for Python:
{
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
This prevents conflicts when working with multi-language projects.
Why This Matters
Format on Save eliminates formatting inconsistencies before they even get committed. Your team follows the same style automatically, and code reviews focus on logic instead of spacing debates.
Key Takeaway: Enabling Format on Save with language-specific formatter settings creates a friction-free workflow where code style is enforced invisibly and consistently.