Visual Studio Code
VSCode
Code Formatting
Programming
Coding Tips

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:

  1. Open the Command Palette: Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) to open the command palette.
  2. Type "Format Document": Start typing Format Document and 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 shortcut Shift+Alt+F (or Shift+Option+F on Mac).
  3. Format on Save: To enable format on save, go to the settings (use the shortcut Ctrl+, or Cmd+, on Mac) and search for Editor: Format On Save and 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:

  1. Find Language-Specific Settings: Open settings (Ctrl+,), and append "[language]" (e.g., "[javascript]") to target specific language settings.
  2. Override Default Formatter: You can set or override the default formatter by specifying it in the settings.json file. For instance:
json
   "[javascript]": {
       "editor.defaultFormatter": "esbenp.prettier-vscode"
   }

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+F on Windows/Linux or Cmd+K Cmd+F on 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

FeatureCommand/ShortcutDescription
Format DocumentShift+Alt+F / Shift+Option+F, Format Document from CPFormats the entire current document using the configured default formatter.
Format On SaveEnable via Editor: Format On SaveAutomatically formats the document on every save.
Format SelectionCtrl+K Ctrl+F / Cmd+K Cmd+FFormats the selected part of the document.
Default Formattersettings.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.


Course illustration
Course illustration

All Rights Reserved.