Advertisement
Advertisement


How do you auto format code in Visual Studio?


Question

I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.

2018/10/08
1
852
10/8/2018 2:09:42 PM

Accepted Answer

To format a selection: Ctrl+K, Ctrl+F

To format a document: Ctrl+K, Ctrl+D

See the pre-defined keyboard shortcuts. (These two are Edit.FormatSelection and Edit.FormatDocument.)

Note for OS X

On OS X use the CMD ⌘ key, not Ctrl:

  • To format a selection: CMD ⌘+K, CMD ⌘+F
  • To format a document: CMD ⌘+K, CMD ⌘+D
2020/06/12
1357
6/12/2020 11:50:36 PM

For Visual Studio 2010/2013/2015/2017/2019

  • Format Document (Ctrl+K,Ctrl+D) so type Ctrl+K, AND THEN Ctrl+D as it is a sequence
  • Format Selection (Ctrl+K,Ctrl+F)

Toolbar Edit -> Advanced (If you can't see Advanced, select a code file in solution explorer and try again)

Your shortcuts might display differently to mine as I am set up for C# coding but navigating via the toolbar will get you to your ones.

If it isn't working, look for errors in your code, like missing brackets which stop auto format from working

2020/06/09

Follow the steps below:

  • Go to menu Tools
  • Go to Options
  • Go to the Text Editor options
  • Click the language of your choice. I used C# as an example.

See the below image:

Enter image description here

2020/06/12

I have installed an extension named "Format document on Save" which formats the whole document every time you save it. For install it in VS15 or 17, on Tools just click the "Extensions and Updates...":

enter image description here

And then just go to "Online" at the left panel and search for "Format document on save" enter image description here

I hope it helps!.

2017/08/30

Even though the question is a bit old, someone might find it handy.

You can define new key bindings by going to Tools -> options -> Environment -> keyboard

enter image description here

2015/03/11

  1. Go to Tools -> Extensions & Updates and type "productivity" in search: 1
  2. Install 'Productivity Power Tools 2015'
  3. Restart VS.
  4. Go to Tools -> Options -> Productivity Power Tools -> Power Commands and check "Format document on save": 2
2017/09/18

Source: https://stackoverflow.com/questions/5755942
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]