When typing in commands in VS Code, trying to be neat can sometimes be the last thing on your mind. Although formatting isn’t required for a program to run, it helps greatly during debugging and trying to locate errors.
In this article, we’ll show you how to format code in VS Code along with other helpful tips for keeping your code well organized.
How to Format Code
VS Code has built-in commands to arrange currently typed code to the standard format. These shortcut keys require no additional extensions and can be used at any time. The shortcuts are as follows:
For PC
Format the entire document:
- Open the file with the code that you wish to format.
- Press “Shift + Alt + F”.
- Save the changes by clicking on “File” in the upper left corner then choosing “Save,” or by pressing “Ctrl + S”.
Formatting selected code only:
- On the file with the lines of code that you wish to format, select the lines by highlighting them with your mouse.
- Press “Ctrl + K”.
- Press “Ctrl + F”.
- Save the changes either by choosing “Save” on the File menu in the upper left corner or by pressing “Ctrl + S”.
Note that if you press “Ctrl + F” without first pressing “Ctrl + K” you’ll open up the Find Menu. If this happens, just close the clicking the “x” button or pressing Esc.
Move lines up or down:
- Move your cursor to the beginning of the line that you wish to move.
- Hold down the Alt Key.
- To move the line upwards, press the up arrow. To move it down, press the down arrow.
- Save your file by choosing it from the File menu in the upper left corner of the window or by pressing “Ctrl + S”.
Change indentation of a single line:
- Move your cursor to the beginning of the line in which you want to change the indention.
- Press “Ctrl + ]” to increase the indentation.
- Press “Ctrl + [“ to decrease the indent.
- Save changes by selecting Save from the File menu or by pressing “Ctrl + S”.
For Mac
Format the entire document:
- Open the file with the code that you wish to format.
- Press “⇧ + ⌥ + F.”
- Save your changes by clicking “File” on the upper left then choosing “Save” or pressing “⌘ + S”.
Formatting selected code only:
- Highlight the part of the document that you wish to format.
- Press “⌘ + K”.
- Press “⌘ + F”.
- Save your file by pressing “⌘ + S” or by choosing “Save” from the File menu on the upper left corner of the window.
Note that using “⌘ + F” without pressing “⌘ + K” beforehand will only open the Find Menu. To close the Find Menu just press “x” on the right side or press Esc.
Move lines up or down:
- Place your cursor at the beginning of the line you want to edit.
- Hold down the “⌥” Key.
- To move a line up, press the up arrow. To move it down, press the down arrow.
- Save your changes by choosing “Save” from the File menu or by pressing “⌘ + S”.
Change indentation of a single line:
- Move your cursor to the beginning of the line in which you want to change the indention.
- Press “⌘ + ]” to increase the indentation.
- Press “⌘ + [“ to decrease the indent.
- Save the file by pressing “⌘ + S” or by choosing “Save” from the File menu on the upper left corner of the window.
VS Code Format Code on Save
VS Code doesn’t have a native command that formats your document on save. Instead, this can be done by installing a formatting extension onto your VS Code application. The most popular of these extensions is Prettier which provides a lot of formatting functionality to VS Code. Prettier can be installed by doing the following:
- Open VS Code.
- Click on the Extensions button located on the left menu. This is the icon that looks like four boxes. Alternatively, you can press “Ctrl + Shit + x” for PC or “⌘ + ⇧ + x” on Mac.
- In the search bar at the top of the menu, type in Prettier.
- Click on the Install button on the bottom right corner of the Prettier icon.
- Wait for the extension to finish the installation.
Before Prettier can begin to automatically format your document on save, you’ll have to set up the extension to activate the feature. This is done by doing the following:
- Open the settings window by pressing “Ctrl +,” on PC or “⌘ +,” on Mac.
- On the search bar type in formatter. This should bring up several formatting settings.
- On the Editor: Default Formatter setting, make sure that the selected extension is Prettier. If there’s no default formatter or VS Code is using another formatter by default, press the dropdown arrow. Select “Prettier – Code Formatter” from the list. Alternatively, Prettier may appear on the list as “esbenp.prettier-vscode”.
- Make sure that the “Editor: Format On Save” option has been checked. If not, toggle the checkmark.
- Type “Prettier” in the settings search bar.
- Scroll down until you find the line “Prettier: Require Config”. Make sure that the checkbox has been ticked. This setting prevents Prettier from formatting documents without a config file. This is handy when you’re looking at downloaded code that may have its own formatting rules. This prevents you from overwriting formatting options unintentionally. Note that untitled files will still be formatted automatically even if this setting is checked.
- You can edit specific Prettier settings depending on your preferences. Once you’re done, you can exit this menu.
As you’ve configured Prettier to automatically format only when there’s a config file, you must create one for each project. This is done by following these steps:
- Select the root of your project on the left menu.
- Click on the new file button to create a config file.
- Name this file “.prettierrc.”
- On the file, just enter {}.
- Prettier will now automatically format your document whenever you save it.
Tips for Keeping Code Organized
- Although indentation isn’t necessary for a program to run, it can help debugging by compartmentalizing code into manageable modules. If-Then statements or nested cases, for example, can benefit from this by making each alternative option visually distinguishable from one another. This comes in handy when you’re dealing with logic errors instead of syntax ones.
- If you’re naming modules or short lines of code, make it a habit in using descriptive titles rather than simply calling it module 1, module 2, etc. This makes it easier to know which part of the code does a particular function.
- It’s always a good idea to use comments to your advantage. Whether you’re including a quick description, or just adding a note for yourself, comments help immensely during debugging.
Organizing Code
Keeping your projects properly formatted not only makes it easier to read, it also helps in identifying errors and keeps your code organized. Although not necessary for a program to run, knowing how to format your files in VS Code is a definite advantage.
Do you know of other ways to format your files in VS Code? Feel free to share your thoughts in the comments section below.
Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.