How do you auto format code in Visual Studio?
I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.
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
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
Read more... Read less...
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
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:
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...":
I hope it helps!.
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
- Go to Tools -> Extensions & Updates and type "productivity" in search:
- Install 'Productivity Power Tools 2015'
- Restart VS.
- Go to Tools -> Options -> Productivity Power Tools -> Power Commands and check "Format document on save":