35

How can I format code in Visual Studio Code in the last update on Windows.

I tried Ctrl+ K, Ctrl + D doesn't work...

I also tried Shift + Alt + F. It didn't work either.

I tried the following solution, but that is not the issue:

How do you format code in Visual Studio Code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I am not on a windows machine right now so I am not 100%. However, you can always find out yourself, Preferences -> Keyboard Shortcuts, search for editor.action.format. – seN Mar 14 '16 at 23:46
  • 1
    Possible duplicate of [How do you format code in Visual Studio Code (VSCode)](https://stackoverflow.com/questions/29973357/how-do-you-format-code-in-visual-studio-code-vscode) – Harry Nov 29 '17 at 10:25
  • @Harry: No, not really. That question (and its answers) has ***unstated assumptions***. Something like *"only for JavaScript, HTML, and JSON"* (the default supported). Whereas this question has unstated assumptions as well, I think. Something like *"for PHP code"* (or *"for C# code"*. Or *"for C++ code"*). – Peter Mortensen Jun 13 '20 at 14:23

6 Answers6

43

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dev pro
  • 620
  • 1
  • 7
  • 12
  • Can you provide the *exact name* of the extension (as the provided words are not specific enough for search)? Even better would be a link to it. – Peter Mortensen Jun 13 '20 at 12:12
11

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.

MD Ashik
  • 9,117
  • 10
  • 52
  • 59
hamiltonia
  • 2,895
  • 1
  • 9
  • 5
7

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

Deb
  • 555
  • 6
  • 6
7

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
1

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.)

Sneftel
  • 40,271
  • 12
  • 71
  • 104
Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
0

Formatting any document in Visual Studio Code requires one to install a formatter plugin, like Prettier - Code formatted by Esben Petersen.

  • Install the plugin: Prettier - Code formatter from the Extensions window in Visual Studio Code.

    • You can open the extensions window from the left aligned sidebar or simply by pressing Ctrl + Shift + X
  • Post installation, from the required page you can try the below

    • Right click and choose Format Document; or
    • Try Alt+Shift+F on Windows

The document should now be properly formatted.

  • PS: You can set "Prettier - Code formatted" as the default formatter for Visual Studio Code by performing the following:
    • Open Settings by pressing Ctrl + , and search for editor.defaultFormatter. Choose esbenp.prettier-vscode from the list and Save. By default, now formatting any document with the above steps will use Prettier as the default formatter.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RICHARD ABRAHAM
  • 2,218
  • 20
  • 26