Skip to content

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

Category
Programming
Covers
VS Code
On this page

Code formatting is essential for developers. A formatted code is easy to understand for other developers and allows us to find issues within the source code quickly. This article will help you to format source code in Visual Studio Code (VSCode) editor on various operating systems.

VSCode – Code Formatting Keyboard Shortcuts

You can format code using the keyboard shortcuts. Use the following key combinations to format code in Visual Studio Code (VSCode).

  • Shift + Alt + F : Windows Users
  • Shift + Option + F: macOS Users
  • Ctrl + Shift + I: Linux Users

VSCode – Code Formatting Option #2

You can also format code using the options. Right-click on your source’s code content and select the format document. In this way the VSCode will select the default formatted for that language.

If you have multiple code formatter extensions installed for a specific language, you need to click the “Format Document With…” option on the right click. Then select the appropriate formatter.

how to format source code in visual studio code
Code formatting in visual studio code

VSCode – Installing code formatters

When you press the formatting shortcut but no formatter is installed for the specific programming language, You will see a popup like the below:

how to format source code in VSCode
Installing source code formatter extension

Click the “Install Formatter…” button, this will search for the available code formatting extension for that programming language. You can install any formatting extension of your choice that supports your language.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top