How do I fix the indentation of selected lines in Visual Studio

Visual Studio-2010Keyboard Shortcuts

Visual Studio-2010 Problem Overview


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

Visual Studio-2010 Solutions


Solution 1 - Visual Studio-2010

Selecting all the text you wish to format and pressing CtrlK, CtrlF shortcut applies the indenting and space formatting.

As specified in the Formatting pane (of the language being used) in the Text Editor section of the Options dialog.

See VS Shortcuts for more.

Solution 2 - Visual Studio-2010

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

Solution 3 - Visual Studio-2010

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.

Solution 4 - Visual Studio-2010

> For the Mac users.

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

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

Solution 5 - Visual Studio-2010

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:
  3. Click on Smart (for resolving conflicts);
  4. Type the Tab Size and Indent Size you want (e.g. 2);
  5. Click on Insert Spaces if you want to replace tabs by spaces;
  6. 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhugomgView Question on Stackoverflow
Solution 1 - Visual Studio-2010user405725View Answer on Stackoverflow
Solution 2 - Visual Studio-2010CRTView Answer on Stackoverflow
Solution 3 - Visual Studio-2010Fetchez la vacheView Answer on Stackoverflow
Solution 4 - Visual Studio-2010Siraj AlamView Answer on Stackoverflow
Solution 5 - Visual Studio-2010CPHPythonView Answer on Stackoverflow