The best answers to the question “How do you auto format code in Visual Studio?” in the category Dev.
I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.
For Visual Studio 2010/2013/2015/2017/2019
- Format Document (Ctrl+K,Ctrl+D), i.e. press&hold Ctrl, press&release K then tap 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
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 macOS
On macOS, use the CMD ⌘ key instead of Ctrl:
- To format a selection: CMD ⌘+K, CMD ⌘+F
- To format a document:
CMD ⌘+K, CMD ⌘+D
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 installing it in Visual Studio 2015 or Visual Studio 2017, on Tools just click the “Extensions and Updates…”:
And then just go to “Online” at the left panel and search for “Format document on save”: