How do you format code in Visual Studio Code (VSCode)?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Visual Studio Code (VSCode) is a highly popular and versatile open-source code editor developed by Microsoft. It supports a wide range of programming languages with powerful features that make coding more efficient and organized. One such feature is code formatting, which automatically tidies up the appearance of your code to make it more readable and maintainable.
How to Format Code in VSCode
Using the Built-In Formatter
VSCode has built-in support for languages like JavaScript, TypeScript, JSON, HTML, and CSS. However, formatting capabilities for other languages can be extended via extensions. To format code:
- Open the Command Palette: Press
Ctrl+Shift+P(orCmd+Shift+Pon Mac) to open the command palette. - Type "Format Document": Start typing
Format Documentand select it when it appears in the dropdown. This will format the current document according to the default formatter set for that file type.Alternatively, you can use the shortcutShift+Alt+F(orShift+Option+Fon Mac). - Format on Save: To enable format on save, go to the settings (use the shortcut
Ctrl+,orCmd+,on Mac) and search forEditor: Format On Saveand check the box. This ensures that every time you save your document, it automatically formats.
Customizing Formatting Rules
To customize formatting settings for a specific language:
- Find Language-Specific Settings: Open settings (
Ctrl+,), and append"[language]"(e.g.,"[javascript]") to target specific language settings. - Override Default Formatter: You can set or override the default formatter by specifying it in the settings.json file. For instance:
Using External Formatters
For languages not supported by default, you can install extensions that provide additional formatting capabilities. Some popular formatter extensions include:
- Prettier: Widely used for formatting JavaScript, CSS, and other web technologies.
- Black: A Python code formatter.
- Clang-Format: Useful for C, C++, and Objective-C code formats.
Keyboard Shortcuts and Commands
Visual Studio Code provides several shortcuts and commands that streamline the formatting process:
- Format Selection: If you only want to format a specific part of your code, you can select the segment and then use
Ctrl+K Ctrl+Fon Windows/Linux orCmd+K Cmd+Fon Mac.
Formatting with Configuration Files
Many formatters use configuration files (like .prettierrc for Prettier or pyproject.toml for Black) that allow developers to set project-specific formatting rules. These files should be placed at the root of your project directory.
Summary Table
| Feature | Command/Shortcut | Description |
| Format Document | Shift+Alt+F / Shift+Option+F, Format Document from CP | Formats the entire current document using the configured default formatter. |
| Format On Save | Enable via Editor: Format On Save | Automatically formats the document on every save. |
| Format Selection | Ctrl+K Ctrl+F / Cmd+K Cmd+F | Formats the selected part of the document. |
| Default Formatter | settings.json (e.g., "editor.defaultFormatter": "id") | Set or override which formatter to use for specific languages. |
| Configuration Files | .prettierrc, pyproject.toml, etc. | Customize formatting rules more granularly with formatter-specific configuration files. |
By properly leveraging the formatting tools and configurations in VSCode, developers can ensure their codebase remains clean, consistent, and easy to manage across different environments and teams. This not only improves readability but also increases productivity and reduces the likelihood of bugs related to syntax and formatting errors.

