Reputation: 12277
I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.
Upvotes: 1225
Views: 2375557
Reputation: 5306
In Visual Studio 2017, 2019, 2022
Format Document is Ctrl + E, D.
But...if you want to add the Format Document button to a tool bar do this:
Now...
Upvotes: 16
Reputation: 5267
For Visual Studio 2010/2013/2015/2017/2019
Toolbar Edit -> Advanced -> Format Document (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
Upvotes: 215
Reputation: 119
In Visual Studio 2022, I use the search bar in the top menu (Feature Search) and type "fix format" then press Enter.
Of course keyboard shortcuts Ctrl+K, Ctrl+D is easier, but often I forgot it!
Upvotes: 2
Reputation: 47
In Visual Studio 2015 and 2017 for C# code.
}
}
Upvotes: 0
Reputation: 27930
With the Continuous Formatting extension (commercial, developed by me), the code is formatted really automatically as you type.
Upvotes: 1
Reputation: 3912
Since Visual Studio 2022 17.1 there is a built-in Feature to run code formatting on save (see Microsoft Devblogs), meaning there is no need to install extensions like "Format document on Save".
Upvotes: 46
Reputation: 1
Using VS 2017 I wanted to format web page HTML which somehow had become entirely left aligned. Even after installing Productivity Power Tools this wouldn't work.
To fix, for HTML I had to go to Tools --> Options --> Text Editor --> HTML (Web Forms) and change; Tabs Indenting = Smart and Tab = Keep Tabs.
Upvotes: -1
Reputation: 3704
Visual Studio 2019 & 2022
or just click Edit => Advanced => Format Document / Format Selection
Upvotes: 59
Reputation: 491
Go to menu Tools → Extensions & Updates and type "productivity" in search:
Install 'Productivity Power Tools 2015'
Restart Visual Studio.
Go to menu Tools → Options → Productivity Power Tools → Power Commands and check "Format document on save":
Note: In VS2022 we don't have power commands.
Upvotes: 49
Reputation: 2080
If it's still not working then you can select your entire document, copy and paste and it will reformat.
So ...
This is the only thing that I have found that works in Visual Studio Community Edition on Mac.
Upvotes: 2
Reputation: 11264
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.
Step (1): Ctrl + A
Step (2): Ctrl + K
Step (3): Ctrl + F
Upvotes: 9
Reputation: 36
You can also try the right click menu (context menu) option to format the selection of the coding document. Take a look at the below screenshot:
Upvotes: 1
Reputation: 614
If you display the HTML Source Editing toolbar, there is a "Format the Whole Document" button as well.
Upvotes: 13
Reputation: 1071
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":
Upvotes: 70
Reputation: 4017
The original question said "I cannot find the setting."
Simple answer is: Look at top menu, then
Edit --> Advanced --> Format Document
You will also see the currently assigned key strokes for that function. Nothing special to memorize. This really helps if you use multiple developer environments on different operating systems.
Upvotes: 5
Reputation: 1171
If you can afford it (or if you're eligible for the 30-day free trial) JetBrains' ReSharper can reformat a whole project directory.
Just install → right-click a directory → select Cleanup Code from the context menu.
Upvotes: 0
Reputation: 21088
Right click:
It works in Visual Studio 2015, maybe earlier version.
Upvotes: 6
Reputation: 113
Just to further Starwfanatic and Ewan's answers, you can customise your IDE to add any button to any toolbar - so you can add the Format button (as the HTML Source Editing toolbar has) to any other toolbar (like Text Editing with all the other edit controls like increase/decrease indent).
Click the arrow to the right of the toolbar → Add or Remove Buttons → Customize... → Commands tab → button.
Document Format and Selection Format are both under the Edit group.
(Tested in Visual Studio 2010 and Visual Studio 2013)
Upvotes: 2
Reputation: 4006
You can define new key bindings by going to Tools → Options → Environment → keyboard:
Upvotes: 31
Reputation: 399
Under menu Tools → Options → Text Editor, then going to the Formatting → General section of whatever language you wish to format you will find General. Check all three formatting check-boxes.
Under menuTools → Options → Text Editor, then going to the TABS section of whatever language you wish to format you will find Indenting. Select Smart and it will activate automatic formatting whenever you use one of the closing elements ; ) } within that block.
There isn't any need for keystrokes.
Upvotes: 8
Reputation: 680
The solution provided in accepted answer does not apply to Microsoft Visual Studio 2012.
In case of Visual Studio 2012, the shortcuts are:
Upvotes: 11
Reputation: 4548
I used to use these combinations. I automated this process on Save of a document. You can try my extension Format Document on Save.
Upvotes: 14
Reputation: 1756
You can also use the CodeMaid Extension.
You can get the extension from Visual Studio Market Place.
Here is the link to the extension. CodeMaid
Upvotes: 0
Reputation: 487
In Visual Studio 2019 , "Code Cleanup" (RunDefaultCodeCleanup) is more advanced (taken from ReSharper): Ctrl + K, Ctrl + E
Options dialog box: Text Editor → C# → Code Style → Formatting
Upvotes: 10
Reputation: 26976
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
.)
On macOS, use the CMD ⌘ key instead of Ctrl:
Upvotes: 1823
Reputation: 10871
Follow the steps below:
See the below image:
Upvotes: 43
Reputation: 581
You can add the buttons to your toolbar by clicking the little drop down arrow to the right of the last toolbar button, select "Add or Remove Buttons" and then click the buttons you want to add a tick to them. The button(s) you select will appear on your toolbar ...
Then you just select text and click the Increase Indent or Decrease Indent buttons. I tested this on Visual Studio 2013 only.
Upvotes: 6