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
- 2. VSCode – Code Formatting Shortcut #2
- 3. Formatter is not installed
- 4. References
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.
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.
3.2 Clicks Yes button, and VSCode will install the autopep8
formatter automatically.
3.3 Try to format the source code again; now, the VSCode will auto-format the Python source code using the installed Python formatter.