How do you auto format code in Visual Studio?

2019-01-07 01:25发布

问题:

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

回答1:

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



回答2:

For Visual Studio 2010/2013/2015/2017

  • 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



回答3:

Follow the steps below:

  • Go to 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:



回答4:

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



回答5:

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...":

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

I hope it helps!.



回答6:

If you display the HTML Source Editing toolbar there is a "Format the Whole Document" button as well



回答7:

  1. Go to Tools -> Extensions & Updates and type "productivity" in search:
  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":


回答8:

The solution provided in accepted answer does not apply to Microsoft Visual Studio 2012.

In case of VS2012 the shortcuts are:

  • For a highlighted block of code: Ctrl + K, Ctrl + F
  • For the document-wide formatting: Ctrl + K, Ctrl + D


回答9:

I used to use these combinations. I automated this process on Save of a document. You can try mine extension Format Document on Save



回答10:

Under Under 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 Tools -> 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.

No need for keystrokes.



回答11:

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.



回答12:

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.



回答13:

Right click:

Works in VS 2015, maybe earlier version.



回答14:

In VS 2017 Format Document is CTRL E + D.

But...if you want to add the Format Document button to a tool bar do this.
Right click on tool bar.
Select "Customize.."
Select the "Commands" Tab.
Select the "Toolbar" radio button.
Select "Text Editor" from the pull down next to the radio button (or what ever tool bar you want the botton on)

Now...

Click the Add Command button.
Categories: Edit
Commands: Document Format
Click OK



回答15:

In newer versions, the shortcut for the document-wide formatting is: Shift + Alt + F



回答16:

With the Continuous Formatting extension (commercial, developed by me), the code is formatted really automatically as you type.



回答17:

Just to further Starwfanatic and Ewan's answers above. 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 > Add Command... button.

Document Format and Selection Format are both under the Edit group.

(Tested in VS2010 and VS2013)



回答18:

Cut/Paste of a section is another quick way (and easy to remember).



回答19:

On mac : On Mac Shift + Option + F

On ubuntu : Ctrl + Shift + I



回答20:

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.



回答21:

In Visual Studio 2015 and 2017 for c# code. 1) Scroll to the end of the file 2) Remove the last "Curly Bracket" } 3) Wait until the line above it shows an error 4) Replace the "Curly Bracket" } fini. :)