开发者

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

开发者 https://www.devze.com 2023-04-09 08:16 出处:网络
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 shortcut

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" to开发者_运维技巧o much. (In the meantime I'll see if I can edit the formatting options to my satisfaction...)


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.


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


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.


For the Mac users.

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

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


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.


Update for Visual Studio 2022: You can click this button to format the entire document:

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

You can also see the keyboard shortcut on hover. Note: this may apply additional formatting other than just indentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号