Introduction
Clean, well-formatted code isn’t just about looks it improves readability, reduces bugs, and makes collaboration easier.
Luckily, VS Code makes code formatting incredibly simple, whether you prefer shortcuts or automatic formatting.
In this guide, you’ll learn how to format code in VS Code, including keyboard shortcuts, settings, and popular extensions like Prettier.
Why Code Formatting Matters
Proper formatting helps you:
- Read code faster
- Avoid syntax mistakes
- Follow team standards
- Look professional as a developer
Most modern teams expect consistent formatting, and VS Code helps you achieve that effortlessly.
Format Code Using VS Code Shortcut
The fastest way to format code is using a keyboard shortcut.
Format Document Shortcut
- Windows / Linux:
Shift + Alt + F - macOS:
Shift + Option + F
This formats the entire file instantly.
Format Selected Code
- Select the code
- Right-click
- Choose Format Selection
Perfect for formatting specific blocks without touching the whole file.
Format Code from Command Palette
You can also format code using the Command Palette:
- Press
Ctrl + Shift + P(Windows/Linux)
orCmd + Shift + P(macOS) - Type Format Document
- Press Enter
This works even if you forget the shortcut.
Enable Auto Format on Save
One of the best features in VS Code is auto-formatting on save.
How to Enable It
- Open Settings (
Ctrl + ,) - Search for Format On Save
- Enable Editor: Format On Save
Now your code formats automatically every time you save the file 💡
Using Prettier to Format Code
Prettieris the most popular formatting extension for VS Code.
Install Prettier
- Open Extensions (
Ctrl + Shift + X) - Search Prettier – Code formatter
- Install it
Set Prettier as Default Formatter
- Open Settings
- Search Default Formatter
- Select Prettier
Now VS Code uses Prettier every time you format.
Example: Prettier Formatting
Before formatting:
function greet( ){console.log("Hello world")}
After formatting:
function greet() {
console.log("Hello world");
}
Clean and consistent ✨
Format Specific Languages
VS Code allows language-specific formatting settings.
Example: JavaScript Only
{
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
You can do this for HTML, CSS, JSON, and more.
Common Formatting Issues (And Fixes)
Format Not Working?
- Install a formatter (Prettier)
- Set default formatter
- Check file language mode (bottom-right)
Conflicting Extensions?
Disable extra formatters to avoid conflicts.
Best Practices for VS Code Formatting
- Use Prettier for consistency
- Enable format on save
- Use project-level
.prettierrcfiles - Format before committing code
These habits make you look like a pro developer.
Conclusion
Formatting code in VS Code is fast, flexible, and powerful. Whether you use shortcuts, auto-format on save, or tools like Prettier, VS Code helps you keep your code clean and readable with minimal effort.
At Developer Hint, we encourage developers to adopt proper formatting early it’s a simple habit that dramatically improves code quality and collaboration.
Discover more from Developer Hint
Subscribe to get the latest posts sent to your email.







Leave a Reply