Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format code in Visual Studio Code on Windows

People also ask

How do I Auto beautify code in Visual Studio?

Auto formatting settings in Visual Studio Show activity on this post. Select the text you want to automatically indent. Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.

How do I enable formatting in Visual Studio?

To access this options page, choose Tools > Options from the menu bar. In the Options dialog box, choose Text Editor > C# > Code Style > Formatting.


If you are working on PHP you should download the PHP extension format code.

Press F1 and type:

ext install PHP Code Format

Then Shift + Alt + F will work.


You can also select the code you want to format and right click, find the format code menu item and it should have the keyboard command listed next to it.

By default it should be Shift + Alt/Option + F.


Simply use Ctrl + Shift + I
Or
Right Click on document and Click on Format Document


If it's for JavaScript, JSON, CSS, Sass, and HTML, there no better way than the Beautify extension. It is simple and very useful!

Once installed the way you do it are described here with good illustrations (to avoid content duplication): How can I format PHP files with HTML markup in Visual Studio Code?

For others languages, you add the appropriate extensions and you can set keybinding (you need to get the appropriate command to execute, and add the keybinding to keybindings.json). I have explained that in the link above. Always read well the documentation of the extensions. There you can find all the possible configurations and so.

For specific languages you can search via Visual Studio Code in the extension marketplace. Enter just Beautify and you will see all the different Beautify extensions for the different languages. Also you can use formatting format formatter keywords, like PHP formatting and so.

Also, you can use the integrated formatting tool of Visual Studio Code. Mostly by clicking right and selecting formatting document, you can also format just a selection.

Here is the shortcut for formatting the whole document:

  • On Windows: Alt + Shift + F
  • On Mac: Shift + Option + F
  • On Ubuntu: Ctrl + Shift + I

As abdullam mentioned in his comment, the shortcut that worked for me is Shift + Alt + F.

(The accepted answer didn't work for me. I am using Visual Studio Code on a Mac. I couldn't find the extension mentioned above i.e ext install PHP Code Format.)