Main Tutorials

How to format source code in Visual Studio Code (VSCode)

This article shows how to format source code in Visual Studio Code (VSCode).

Tables of contents

1. VSCode – Code Formatting Shortcuts

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations:

  • On Windows Shift + Alt + F
  • On macOS Shift + Option + F
  • On Linux Ctrl + Shift + I

Note
It works for all source codes like HTML, Java, Python, JavaScript ad, etc., it is working the same way.

2. VSCode – Code Formatting Shortcut #2

Alternatively, click on the source code editor, right clicks and select the Format Document option.

format document vscode

3. Formatter is not installed

If we press the code formatting shortcuts and VSCode can’t find any formatter for the specified source code, and VSCode will pop out a dialog at the right-bottom and ask you to install the suggested formatter for the source code;

3.1 Below example shows the VSCode request to install the Python formatter autopep8.

vscode python formatter

3.2 Clicks Yes button, and VSCode will install the autopep8 formatter automatically.

vscode python formatter

3.3 Try to format the source code again; now, the VSCode will auto-format the Python source code using the installed Python formatter.

4. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments