How do you auto format code in Visual Studio?

2019-01-07 01:32发布

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

21条回答
做自己的国王
2楼-- · 2019-01-07 01:52

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

查看更多
老娘就宠你
3楼-- · 2019-01-07 01:53

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

查看更多
神经病院院长
4楼-- · 2019-01-07 01:54

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)

查看更多
再贱就再见
5楼-- · 2019-01-07 01:57

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:

enter image description here

查看更多
SAY GOODBYE
6楼-- · 2019-01-07 01:57

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

查看更多
看我几分像从前
7楼-- · 2019-01-07 01:59

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

enter image description here

Then you just select text and click the Increase Indent or Decrease Indent buttons. I tested this on Visual Studio 2013 only.

查看更多
登录 后发表回答