Format code in vs code

Microsoft Visual studio code, also called vs code is a very popular and light weight IDE for writing code.
It supports all major languages such as HTML, java, python, javascript etc., and is a very popular among developers.

One of the primary requirement of writing code is to format it so that the lines are indented properly.
In this article, we will look at 2 different ways to format code in vs code.

Method 1: Using shortcut
Default shortcut for formatting code in vs code on Windows is

Alt + Shift + F

Pressing these 3 keys together will format the current file, no matter whatever language it is.

If you are on Mac, then replace Alt key with Option.
For linux, the shortcut is

Ctrl + Shift + I

I stands for indent.

Change default shortcut
Shortcut to format code in vs code is configurable, meaning you can change it as per convenience.
Follow below steps.

1. Press Ctrl + Shift + P, and search for “Shortcut”.
2. Select Open Keyboard Shortcuts option.
3. In the window that appears, search for “format”. You will see an option Format Document as shown below.
format code shortcut in vs code

4. Hover over this option and you will see an edit icon at the left.
5. Click it and press the keys that you want as a shortcut to format code in vs code, one after another.
6. Press enter and this shortcut will be set to format code.

Method 2: Context menu option
Right click on the file that you want to format.
You will see an option “Format Document”.

Clicking on this option will format the current file code in vs code.

Hope the article helped you out in formatting code in vs code.