[visual-studio-2010] How do I fix the indentation of selected lines in Visual Studio

In vim I can use = to reindent badly indented lines so

foo;
  bar;
 baz;

becomes

foo;
bar;
baz;

Is there an equivalent keyboard-shortcut for visual studio? Where can I find a list of such shortcuts for future reference?


Edit: Is there a way to do fix just the horizontal indentation? There are some cases where the CTRLK,CtrlF is "fixing" too much. (In the meantime I'll see if I can edit the formatting options to my satisfaction...)

This question is related to visual-studio-2010 keyboard-shortcuts

The answer is


For the Mac users.

For selecting all of the code in the document => cmd+A

For formatting selected code => cmd+K, cmd+F


Selecting the text to fix, and CtrlK, CtrlF shortcut certainly works. However, I generally find that if a particular method (for instance) has it's indentation messed up, simply removing the closing brace of the method, and re-adding, in fact fixes the indentation anyway, thereby doing without the need to select the code before hand, ergo is quicker. ymmv.


I like Ctrl+K, Ctrl+D, which indents the whole document.


To fix the indentation and formatting in all files of your solution:

  1. Install the Format All Files extension => close VS, execute the .vsix file and reopen VS;
  2. Menu Tools > Options... > Text Editor > All Languages > Tabs:
    1. Click on Smart (for resolving conflicts);
    2. Type the Tab Size and Indent Size you want (e.g. 2);
    3. Click on Insert Spaces if you want to replace tabs by spaces;
  3. In the Solution Explorer (Ctrl+Alt+L) right click in any file and choose from the menu Format All Files (near the bottom).

This will recursively open and save all files in your solution, setting the indentation you defined above.

You might want to check other programming languages tabs (Options...) for Code Style > Formatting as well.