What is Format on Save in VS Code
The “Format on Save” setting in Visual Studio Code (VS Code) is a built-in feature that automatically cleans up and structures your code every time you save a file. By instantly applying formatting rules—such as adjusting indentation, adding missing semicolons, and wrapping long lines—this setting ensures consistent code style, saves manual editing time, and helps prevent syntax errors before you commit your work.
How Format on Save Works
When you enable Format on Save, pressing the save shortcut
(Ctrl + S on Windows/Linux or Cmd + S on
macOS) triggers VS Code to run your active code formatter on the active
document.
The editor looks at your configured formatting tool for that specific programming language—such as Prettier for JavaScript/HTML/CSS, Black for Python, or the built-in TypeScript formatter—and applies its rules instantly. This happens in milliseconds, right before the file is written to your storage drive.
Key Benefits of Enabling the Setting
- Enforced Code Consistency: It guarantees that your code always adheres to your chosen style guide, whether you are working alone or in a large development team.
- Time Efficiency: You no longer need to manually
format your code using keyboard shortcuts (like
Shift + Alt + F) or worry about manual indentation while writing. - Cleaner Git Diffs: Because the code is formatted automatically before every save, you avoid commits cluttered with accidental whitespace changes or inconsistent line endings.
How to Enable Format on Save in VS Code
You can activate this feature in two different ways:
Method 1: Using the Settings UI
- Open Settings using
Ctrl + ,(orCmd + ,on macOS). - In the search bar at the top, type Format on Save.
- Locate the checkbox labeled Editor: Format On Save.
- Check the box to enable it.
Method 2: Editing
settings.json
If you prefer configuring VS Code via code, you can add the following
line to your user or workspace settings.json file:
"editor.formatOnSave": trueLanguage-Specific Formatting
If you do not want to format every single language on save, you can
configure the setting for specific languages in your
settings.json file. For example, to enable it only for
Python and disable it for Markdown, you would write:
"[python]": {
"editor.formatOnSave": true
},
"[markdown]": {
"editor.formatOnSave": false
}