简体   繁体   English

Visual Studio 是否有格式代码快捷方式?

[英]Is there a format code shortcut for Visual Studio?

In Eclipse there is a shortcut, Ctrl + Shift + F , that re-indents code and fixes comments and blank lines.Eclipse中有一个快捷键Ctrl + Shift + F ,它可以重新缩进代码并修复注释和空行。 Is there an equivalent for Visual Studio 2010? Visual Studio 2010 是否有等价物?

Visual Studio with C# key bindings带有C#键绑定的 Visual Studio

To answer the specific question, in C# you are likely to be using the C# keyboard mapping scheme, which will use these hotkeys by default:要回答具体问题,在 C# 中您可能会使用 C# 键盘映射方案,默认情况下将使用这些热键:

Ctrl + E , Ctrl + D to format the entire document. Ctrl + ECtrl + D格式化整个文档。

Ctrl + E , Ctrl + F to format the selection. Ctrl + ECtrl + F格式化选择。

You can change these in menu ToolsOptionsEnvironmentKeyboard (either by selecting a different "keyboard mapping scheme", or binding individual keys to the commands "Edit.FormatDocument" and "Edit.FormatSelection").您可以在菜单工具选项环境键盘中更改这些(通过选择不同的“键盘映射方案”,或将单个键绑定到命令“Edit.FormatDocument”和“Edit.FormatSelection”)。

If you have not chosen to use the C# keyboard mapping scheme, then you may find the key shortcuts are different .如果您没有选择使用 C# 键盘映射方案,那么您可能会发现快捷键不同 For example, if you are not using the C# bindings, the keys are likely to be:例如,如果您不使用 C# 绑定,则键可能是:

Ctrl + K + D (Entire document) Ctrl + K + D (整个文档)

Ctrl + K + F (Selection only) Ctrl + K + F (仅限选择)

To find out which key bindings apply in your copy of Visual Studio, look in menu EditAdvanced menu - the keys are displayed to the right of the menu items, so it's easy to discover what they are on your system.要找出适用于的 Visual Studio 副本的键绑定,请查看菜单编辑高级菜单- 键显示在菜单项的右侧,因此很容易发现它们在您的系统上是什么。


(Please do not edit this answer to change the key bindings above to what your system has!) (请不要编辑此答案以将上面的键绑定更改为您的系统所拥有的!)

Ctrl + K + D (Entire document) Ctrl + K + D (整个文档)

Ctrl + K + F (Selection only) Ctrl + K + F (仅限选择)

尝试Ctrl + K + D (不要在两者之间抬起Ctrl键)。

Yes, you can use the two-chord hotkey ( Ctrl + K , Ctrl + F if you're using the General profile) to format your selection.是的,您可以使用双和弦热键( Ctrl + K ,如果您使用的是常规配置文件,则为Ctrl + F )来格式化您的选择。

Other formatting options are under menu EditAdvanced , and like all Visual Studio commands, you can set your own hotkey via menu ToolsOptionsEnvironmentKeyboard (the format selection command is called Edit.FormatSelection ).其他格式化选项位于菜单EditAdvanced下,并且像所有 Visual Studio 命令一样,您可以通过菜单ToolsOptionsEnvironmentKeyboard设置自己的热键(格式选择命令称为Edit.FormatSelection )。

Formatting doesn't do anything with blank lines, but it will indent your code according to some rules that are usually slightly off from what you probably want.格式化对空行没有任何作用,但它会根据一些通常与您可能想要的内容略有不同的规则缩进您的代码。

ReSharper - Ctrl + Alt + F ReSharper - Ctrl + Alt + F

Visual Studio 2010 - Ctrl + K , Ctrl + D Visual Studio 2010 - Ctrl + KCtrl + D

To align the text in the proper format -以正确的格式对齐文本 -

  • Ctrl + K + D for front end pages like .aspx or .cshtml Ctrl + K + D用于.aspx.cshtml等前端页面

  • Ctrl + K + F for a .cs page .cs页面的Ctrl + K + F

But observe to press all buttons in sequence...但请注意按顺序按下所有按钮...

Format the entire document:格式化整个文档:

Ctrl + K + D Ctrl + K + D

Simply简单地

For Visual Studio Code Use ALt + Shift + F对于Visual Studio Code ,使用ALT + Shift + F

for Visual Studio IDE Press Ctrl + K followed by Ctrl + D对于Visual Studio IDECtrl + K ,然后按Ctrl + D


It will beautify/format your entire file.它将美化/格式化您的整个文件。

选择文档中的所有文本,然后按Ctrl + E + D

Right click on the code, and you have "Format Document".右键单击代码,您将获得“格式化文档”。 In my case it is Ctrl + Shift + I就我而言,它是Ctrl + Shift + I

在此处输入图像描述

Change these shortcuts in Visual Studio在 Visual Studio 中更改这些快捷方式

Tools → Options → Environment → Keyboard

and then change the command然后更改命令

"Edit.FormatDocument" or "Edit.FormatSelection"

assign the same shortcut alt + shift +f as in visual studio code in order to not remember another one and confuse between each other.分配与 Visual Studio 代码中相同的快捷键alt + shift +f ,以免记住另一个快捷键并相互混淆。

Most easy, try once最简单,试一次

  1. Select all codes using :- Ctrl + A使用以下命令选择所有代码:- Ctrl + A
  2. Press :- Ctrl + K + D按:- Ctrl + K + D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM