VOOZH about

URL: https://itsfoss.com/auto-indent-vs-code/

โ‡ฑ How to Automatically Indent Your Code in Visual Studio Code


๐Ÿ‘ Ghostboard pixel

How to Automatically Indent Your Code in Visual Studio Code

Format your code correctly. Learn how to automatically indent code in Visual Studio Code.
Sagar Sharma
3 min read
๐Ÿ‘ Warp Terminal

The indent in code refers to the space you have at the beginning of the code line.

If you want to indent multiple lines at once, select them and press Ctrl+] to add the indent or Ctrl+[ to remove it. Pressing the same keys again will add (or move back) indent again.

But this is the manual method. You have to identify and select the lines by yourself.

Like other code editors and IDEs, VS Code allows you to indent your code automatically.

You can set tabs or spaces or whatever you prefer for the indentation.

Sounds good? Letโ€™s see how to do it.

Enable automatic indent in VS Code

There are multiple ways you can achieve this. In this guide, I will show you three ways to indent your code in visual studio code automatically.

Method 1: Configuring global user settings

You can access the global user settings via the command pallet. Use Ctrl + Shift + P to open the command pallet and search for Open User Settings and hit enter:

๐Ÿ‘ access user setting from command pallet in vscode

It will open up the settings. From there, you will have to search for Auto Indent and choose Full as an indent option in Editor: Auto Indent:

๐Ÿ‘ enable auto indent from global user settings in vscode

And the automatic indent is enabled and applied to every opened file in VSCode.

Method 2: Using linter or formatter for automatic indent in VS Code

In this method, you will be required to add extensions such as a code formatter or linter to have the desired results.

Linters will identify the errors in code, whereas formatters will only format your code to make it more readable. You can search for code formatters in the VSCode marketplace specific to your programming language.

And here are some of my favorite code formatters and linters for widely popular languages:

  • C/C++: For C and C++ programming language.
  • PHP: For PHP.
  • markdownlint: For markdown files.
  • Python: For Python programming language.
  • ESLint: For JSON and javascript.
  • Beautify: For javascript, JSON, CSS, Sass, and HTML.

Once you are done adding a formatter for your preferred programming language, you can press Ctrl _ Shift + I to format the code.

Similarly, you can use do the same using the command pallet. Press Ctrl + Shift + P to and search for Format document, and hit enter.

๐Ÿ‘ indent code in VSCode

Method 3: Enable auto indent while saving the file

VSCode allows you to format your code while saving it with a little tweak. Let me show you how.

Press Ctrl + , and it will open the user settings prompt. From there, search for Format On Save:

๐Ÿ‘ enable format on save option

And from now on, your files will add an indent automatically when you save them.

Wrapping Up

Knowing and practicing the keyboard shortcuts help you work faster. Here are some interesting ones you should know.

In this guide, I explained how you could add an indent automatically in VSCode. I would recommend using the second method for better flexibility.

I hope you will find this guide helpful and if you have any queries or suggestions, let me know in the comments.

About the author

Sagar Sharma

A software engineer who loves to write about his experience with Linux. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.

Read next

Switching to Root User in Ubuntu

Get Process Name From PID in Linux

SSH to Port Other Than 22

Check File Size in Linux Command Line

How to List Users in Linux

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

๐Ÿ‘ itsfoss happy penguin