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?

5
  • 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, 2010 at 18:29
  • @Madman: I am using ViEmu, VAssitX. Its a C# source file. Now surprisely It works fine.
    – Jichao
    Oct 19, 2010 at 4:30
  • 2
    you just need to select all the text/code before doing CTRL+K, CTRL+F, then it should work
    – xus
    Apr 26, 2012 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 7, 2014 at 15:29
  • Possible duplicate of How do you auto format code in Visual Studio?
    – Archmede
    Feb 15, 2018 at 5:02

7 Answers 7

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

4
  • 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, 2013 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, 2014 at 7:18
  • lol MS, "Advanced", because only "Advanced" users would want to format their code???
    – kmarsh
    Mar 18, 2015 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. Jun 24, 2015 at 17:14
47

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

enter image description here

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.

7

In 2010 it is ctrl +k +d for indentation

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.

2
  • Set Options > C/C++ > Formatting > Automatic Indentation on Tab to True and press TAB. Oct 21, 2013 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. Aug 11, 2015 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.

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.