116

How can I automatically indent source code in Visual Studio 2010?

I have used Ctrl + K, Ctrl + F, but it does not work; is there any other way/plugin to do this?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Jichao
  • 40,341
  • 47
  • 125
  • 198
  • 1
    What type of source code? If it doesn't work it's either corrupted VS install, or nonstandard file. Or different keyboard shortcut defaults, try the edit menu. – Coder Oct 18 '10 at 18:29
  • @Madman: I am using ViEmu, VAssitX. Its a C# source file. Now surprisely It works fine. – Jichao Oct 19 '10 at 04:30
  • 2
    you just need to select all the text/code before doing CTRL+K, CTRL+F, then it should work – xus Apr 26 '12 at 11:56
  • 1
    Code in VB.NET :-) ... On a serious note it still amazes me that certain features like this are not coalesced into both languages – Matt Wilko Jan 07 '14 at 15:29
  • Possible duplicate of [How do you auto format code in Visual Studio?](https://stackoverflow.com/questions/5755942/how-do-you-auto-format-code-in-visual-studio) – Archmede Feb 15 '18 at 05:02

7 Answers7

186

Ctrl+E, D - Format whole doc
Ctrl+K, Ctrl+F - Format selection

Also available in the menu via Edit|Advanced.

Thomas

Edit-
Ctrl+K, Ctrl+D - Format whole doc in VS 2010

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Thomas Weller
  • 11,631
  • 3
  • 26
  • 34
  • 1
    I have tried both ways, and from the Edit|Advanced menu, and they are not doing anything to my source code. Other options like line indent are working. What could be wrong? – Chucky Jul 12 '13 at 11:06
  • Chucky, have you considered removing Visual Studio and doing a clean install? I have a few times made so many changes that I've ruined it. – Simply G. Jan 24 '14 at 07:18
  • lol MS, "Advanced", because only "Advanced" users would want to format their code??? – kmarsh Mar 18 '15 at 13:39
  • Side note: shortcut depends on your keyboard layout, so if one does not work - check what Edit->Advanced says. Indeed you can customize it to you liking in Tools->Options->Environment->Keyboard. – Alexei Levenkov Jun 24 '15 at 17:14
47

In 2010 it is Ctrl+k, Ctrl+d. See image below.

enter image description here

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
gooddadmike
  • 2,329
  • 4
  • 26
  • 48
9

In Visual Studio 2010

Ctrl +k +d indent the complete page.

Ctrl +k +f indent the selected Code.

For more help visit : http://msdn.microsoft.com/en-us/library/da5kh0wa.aspx

every thing is there.

general03
  • 855
  • 1
  • 10
  • 33
Armaan
  • 2,472
  • 2
  • 20
  • 22
7

In 2010 it is ctrl +k +d for indentation

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Ksnrg
  • 147
  • 1
  • 2
  • 9
1

Also, there's the handy little "increase indent" and "decrease indent" buttons. If you highlight a block of code and click those buttons the entire block will indent.

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
  • Set `Options > C/C++ > Formatting > Automatic Indentation on Tab` to `True` and press `TAB`. – Steve Pitchers Oct 21 '13 at 13:29
  • I was attempting to compare an XML file to the checked in version in VS 2010, but the indent had changed, making it impossible to see real changes. Using the format feature did not work, but 'increase indent' did. – mono código Aug 11 '15 at 23:15
0

I have tried both ways, and from the Edit|Advanced menu, and they are not doing anything to my source code. Other options like line indent are working. What could be wrong? – Chucky Jul 12 '13 at 11:06

Sometimes if it doesnt work, try to select a couple lines above and below or the whole block of code (whole function, whole cycle, whole switch, etc.), so that it knows how to indent.

Like for example if you copy/paste something into a case statement of a switch and it has wrong indentation, you need to select the text + the line with the case statement above to get it to work.

JoeSham
  • 3
  • 3
0

It may be worth noting that auto-indent does not work if there are syntax errors in the document. Get rid of the red squigglies, and THEN try CTRL+K, CTRL+D, whatever...

LionelGoulet
  • 557
  • 2
  • 13