visual-studio 如何自动缩进源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3958903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:37:31  来源:igfitidea点击:

How to automatically indent source code?

visual-studio-2010visual-studioindentation

提问by Jichao

How can I automatically indent source code in Visual Studio 2010?

如何在 Visual Studio 2010 中自动缩进源代码?

I have used Ctrl+ K, Ctrl+ F, but it does not work; is there any other way/plugin to do this?

我用过Ctrl+ K, Ctrl+ F,但它不起作用;有没有其他方法/插件可以做到这一点?

回答by Thomas Weller

Ctrl+E, D- Format whole doc
Ctrl+K, Ctrl+F- Format selection

Ctrl+ E, D- 格式化整个文档
Ctrl+ K, Ctrl+ F- 格式选择

Also available in the menu via Edit|Advanced.

也可通过Edit|Advanced在菜单中使用。

Thomas

托马斯

Edit-
Ctrl+K, Ctrl+D- Format whole doc in VS 2010

编辑 -
Ctrl+ K, Ctrl+ D- 在 VS 2010 中格式化整个文档

回答by gooddadmike

In 2010 it is Ctrl+k, Ctrl+d. See image below.

2010 年是Ctrl+ k, Ctrl+ d。见下图。

enter image description here

在此处输入图片说明

回答by Armaan

In Visual Studio 2010

在 Visual Studio 2010 中

Ctrl+k+dindent the complete page.

Ctrl+ k+d缩进完整的页面。

Ctrl+k+findent the selected Code.

Ctrl+ k+f缩进所选择的代码。

For more help visit : http://msdn.microsoft.com/en-us/library/da5kh0wa.aspx

如需更多帮助,请访问:http: //msdn.microsoft.com/en-us/library/da5kh0wa.aspx

every thing is there.

每样东西都在那里。

回答by Ksnrg

In 2010 it is ctrl+k+dfor indentation

2010年,它是ctrl+ k+d缩进

回答by Tyler Treat

Also, there's the handy little "increase indent" and "decrease indent" buttons. If you highlight a block of code and click those buttons the entire block will indent.

此外,还有方便的小“增加缩进”和“减少缩进”按钮。如果突出显示代码块并单击这些按钮,则整个代码块都会缩进。

回答by JoeSham

I have tried both ways, and from the Edit|Advanced menu, and they are not doing anything to my source code. Other options like line indent are working. What could be wrong? – Chucky Jul 12 '13 at 11:06

我已经尝试了两种方法,从 Edit|Advanced 菜单,他们没有对我的源代码做任何事情。行缩进等其他选项正在起作用。可能有什么问题?– 查基 2013 年 7 月 12 日 11:06

Sometimes if it doesnt work, try to select a couple lines above and below or the whole block of code (whole function, whole cycle, whole switch, etc.), so that it knows how to indent.

有时如果它不起作用,请尝试选择上下几行或整个代码块(整个函数,整个循环,整个开关等),以便它知道如何缩进。

Like for example if you copy/paste something into a case statement of a switch and it has wrong indentation, you need to select the text + the line with the case statement above to get it to work.

例如,如果您将某些内容复制/粘贴到 switch 的 case 语句中并且缩进错误,则需要选择文本 + 上面 case 语句所在的行才能使其工作。

回答by LionelGoulet

It may be worth noting that auto-indent does not work if there are syntax errors in the document. Get rid of the red squigglies, and THEN try CTRL+K, CTRL+D, whatever...

值得注意的是,如果文档中存在语法错误,自动缩进将不起作用。摆脱红色波浪线,然后尝试 CTRL+K、CTRL+D 等等...