如何修复 VisualStudio 中选定行的缩进

Vim中,我可以使用 =来缩进有严重缩进的行

foo;
bar;
baz;

变成了

foo;
bar;
baz;

视觉工作室是否有相同的快捷键? 我在哪里可以找到这些快捷键的列表,以供将来参考?


编辑: 有没有办法只修复水平缩进?有些情况下,CTRLKCtrlF是“固定”太多。(与此同时,我将看看是否可以编辑格式选项,以满足我的要求...)

203215 次浏览

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.

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.

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:

Toolbar button in Visual Studio

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