如何在visualstudio中自动格式化代码?

时间:2023-01-14 23:34:52

I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.

我知道Visual Studio可以自动设置我的方法和循环缩进,但是我找不到设置。

20 个解决方案

#1


874  

To format a selection: Ctrl+K, Ctrl+F

格式化一个选择:Ctrl+K, Ctrl+F。

To format a document: Ctrl+K, Ctrl+D

格式化文档:Ctrl+K, Ctrl+D。

See the pre-defined keyboard shortcuts. (These two are Edit.FormatSelection and Edit.FormatDocument.)

参见预定义的键盘快捷键。(这两个是编辑。FormatSelection和Edit.FormatDocument)。

#2


112  

For Visual Studio 2010/2013/2015/2017

针对Visual Studio 2010/2013/2015/2017

  • Format Document (Ctrl+K,Ctrl+D) so type Ctrl+K, AND THEN Ctrl+D as it is a sequence
  • 格式化文档(Ctrl+K,Ctrl+D),按Ctrl+K,然后按Ctrl+D按顺序排列。
  • Format Selection (Ctrl+K,Ctrl+F)
  • 格式选择(Ctrl + K,Ctrl + F)

Toolbar Edit -> Advanced (If you can't see Advanced, select a code file in solution explorer and try again)

工具栏编辑->高级(如果您看不到高级,请在解决方案资源管理器中选择一个代码文件并再次尝试)

Your shortcuts might display differently to mine as I am set up for C# coding but navigating via the toolbar will get you to your ones.

当我为c#编码而设置时,你的快捷方式可能会和我的不同,但是通过工具栏导航会让你找到你的快捷键。

If it isn't working, look for errors in your code, like missing brackets which stop auto format from working

如果它不工作,在代码中查找错误,比如缺少了阻止自动格式工作的括号。

#3


32  

Follow the steps below:

遵循下面的步骤:

  • Go to Tools
  • 去工具
  • Go to Options
  • 去选择
  • Go to the Text Editor options
  • 切换到文本编辑器选项。
  • Click the language of your choice. I used C# as an example.
  • 点击你选择的语言。我以c#为例。

See the below image:

看到下面的图片:

如何在visualstudio中自动格式化代码?

#4


25  

Even though the question is a bit old, someone might find it handy.

尽管这个问题有点旧,但有人可能会发现它很方便。

You can define new key bindings by going to Tools -> options -> Environment -> keyboard

您可以通过使用工具->选项->环境->键盘来定义新的键绑定。

如何在visualstudio中自动格式化代码?

#5


15  

I have installed an extension named "Format document on Save" which formats the whole document every time you save it. For install it in VS15 or 17, on Tools just click the "Extensions and Updates...":

我已经安装了一个名为“保存格式文档”的扩展,每次保存它时,它都会格式化整个文档。在VS15或17中安装它,工具只需点击“扩展和更新…”

如何在visualstudio中自动格式化代码?

And then just go to "Online" at the left panel and search for "Format document on save" 如何在visualstudio中自动格式化代码?

然后在左边的面板上“在线”搜索“保存格式文档”

I hope it helps!.

我希望它可以帮助!

#6


9  

If you display the HTML Source Editing toolbar there is a "Format the Whole Document" button as well

如果你显示的是HTML源码编辑工具栏,也会有一个“整个文档的格式”按钮。

#7


8  

The solution provided in accepted answer does not apply to Microsoft Visual Studio 2012.

在接受的答案中提供的解决方案不适用于Microsoft Visual Studio 2012。

In case of VS2012 the shortcuts are:

在VS2012的情况下,捷径是:

  • For a highlighted block of code: Ctrl + K, Ctrl + F
  • 对于高亮显示的代码块:Ctrl + K, Ctrl + F。
  • For the document-wide formatting: Ctrl + K, Ctrl + D
  • 对于文档范围格式:Ctrl + K, Ctrl + D。

#8


7  

I used to use these combinations. I automated this process on Save of a document. You can try mine extension Format Document on Save

我曾经使用过这些组合。我在保存文档时自动执行此过程。你可以试试我的扩展格式文件。

#9


6  

  1. Go to Tools -> Extensions & Updates and type "productivity" in search: 如何在visualstudio中自动格式化代码?
  2. 去工具->扩展和更新和类型“生产力”在搜索:
  3. Install 'Productivity Power Tools 2015'
  4. 安装“2015年生产力工具”
  5. Restart VS.
  6. 重启VS。
  7. Go to Tools -> Options -> Productivity Power Tools -> Power Commands and check "Format document on save": 如何在visualstudio中自动格式化代码?
  8. 去工具->选项->生产力工具->电源命令和检查“格式文件保存”:

#10


5  

Under Under Tools -> Options -> Text Editor, then going to the Formatting -> General section of whatever language you wish to format you will find General. Check all three formatting check-boxes.

在下面的工具->选项->的文本编辑器,然后进入格式化->的任何语言你希望格式化你将找到通用。检查所有三种格式的复选框。

Under Tools -> Options -> Text Editor, then going to the TABS section of whatever language you wish to format you will find Indenting. Select Smart and it will activate automatic formatting whenever you use one of the closing elements ; ) } within that block.

在工具栏->选项->文本编辑器,然后进入你想要格式化的任何语言的选项卡部分,你会发现缩进。选择Smart,当您使用一个关闭元素时,它将激活自动格式;在那块}。

No need for keystrokes.

不需要按键。

#11


4  

You can add the buttons to your toolbar by clicking the little drop down arrow to the right of the last toolbar button, select "Add or Remove Buttons" and then click the buttons you want to add a tick to them. The button(s) you select will appear on your toolbar ...

您可以通过单击最后一个工具栏按钮右边的小箭头按钮,将按钮添加到工具栏中,选择“添加或删除按钮”,然后单击要向它们添加一个标记的按钮。你选择的按钮会出现在你的工具栏上。

如何在visualstudio中自动格式化代码?

Then you just select text and click the Increase Indent or Decrease Indent buttons. I tested this on Visual Studio 2013 only.

然后你只需选择文本并点击增加缩进或减少缩进按钮。我只在Visual Studio 2013上测试过。

#12


4  

Right click:

右键点击:

如何在visualstudio中自动格式化代码?

Works in VS 2015, maybe earlier version.

在VS 2015,可能更早的版本。

#13


4  

The original question said "I cannot find the setting."

最初的问题是:“我找不到布景。”

Simple answer is: Look at top menu, then

简单的回答是:看看上面的菜单。

Edit --> Advanced --> Format Document

编辑->高级->格式文件。

You will also see the currently assigned key strokes for that function. Nothing special to memorize. This really helps if you use multiple developer environments on different operating systems.

您还将看到该函数当前指定的键击。没有什么特别的记忆。如果在不同的操作系统上使用多个开发人员环境,这将非常有用。

#14


2  

In VS 2017 Format Document is CTRL E + D.

在VS 2017格式文件是CTRL + D。

But...if you want to add the Format Document button to a tool bar do this.
Right click on tool bar.
Select "Customize.."
Select the "Commands" Tab.
Select the "Toolbar" radio button.
Select "Text Editor" from the pull down next to the radio button (or what ever tool bar you want the botton on)

但是…如果您想要将格式文档按钮添加到工具栏中,请执行此操作。右键单击工具栏。选择“定制. .”选择“命令”选项卡。选择“工具栏”单选按钮。选择“文本编辑器”,从下拉到单选按钮(或者是你想要的工具栏)

Now...

现在…

Click the Add Command button.
Categories: Edit
Commands: Document Format
Click OK

单击Add命令按钮。类别:编辑命令:文档格式点击确定。

#15


1  

With the Continuous Formatting extension (commercial, developed by me), the code is formatted really automatically as you type.

随着持续格式化扩展(由我开发的商业模式),代码会随着您的类型自动地进行格式化。

#16


1  

Just to further Starwfanatic and Ewan's answers above. You can customise your IDE to add any button to any toolbar - so you can add the Format button (as the HTML Source Editing toolbar has) to any other toolbar (like Text Editing with all the other edit controls like increase/decrease indent).

只是为了进一步了解Starwfanatic和Ewan的答案。您可以定制您的IDE,以向任何工具栏添加任何按钮——因此您可以将Format按钮(作为HTML源代码编辑工具栏)添加到任何其他工具栏(如文本编辑和其他所有编辑控件,如增加/减少缩进)。

Click the arrow to the right of the toolbar > Add or Remove Buttons > Customize... > Commands tab > Add Command... button.

单击工具栏>右边的箭头,添加或删除按钮>自定义…>命令tab >添加命令…按钮。

Document Format and Selection Format are both under the Edit group.

文档格式和选择格式都在编辑组中。

(Tested in VS2010 and VS2013)

(VS2010和VS2013测试)

#17


1  

Cut/Paste of a section is another quick way (and easy to remember).

剪切/粘贴一个部分是另一个快速的方法(而且容易记住)。

#18


1  

In newer versions, the shortcut for the document-wide formatting is: Shift + Alt + F

在较新的版本中,文档范围格式的快捷方式是:Shift + Alt + F。

#19


0  

If you can afford it (or if you're eligible for the 30-day free trial) Jetbrains ReSharper can reformat a whole project directory.

如果你能负担得起(或者如果你有资格享受30天免费试用),Jetbrains ReSharper可以重新格式化整个项目目录。

Just install -> Right-click a directory -> select Cleanup Code from the context menu.

只需安装->右键单击一个目录->从上下文菜单中选择清除代码。

#20


0  

In Visual Studio 2015 and 2017 for c# code. 1) Scroll to the end of the file 2) Remove the last "Curly Bracket" } 3) Wait until the line above it shows an error 4) Replace the "Curly Bracket" } fini. :)

在Visual Studio 2015和2017的c#代码。1)滚动到文件的末尾,删除最后一个“花括号”,直到上面的行显示一个错误4)替换“花括号”}fini。:)

#1


874  

To format a selection: Ctrl+K, Ctrl+F

格式化一个选择:Ctrl+K, Ctrl+F。

To format a document: Ctrl+K, Ctrl+D

格式化文档:Ctrl+K, Ctrl+D。

See the pre-defined keyboard shortcuts. (These two are Edit.FormatSelection and Edit.FormatDocument.)

参见预定义的键盘快捷键。(这两个是编辑。FormatSelection和Edit.FormatDocument)。

#2


112  

For Visual Studio 2010/2013/2015/2017

针对Visual Studio 2010/2013/2015/2017

  • Format Document (Ctrl+K,Ctrl+D) so type Ctrl+K, AND THEN Ctrl+D as it is a sequence
  • 格式化文档(Ctrl+K,Ctrl+D),按Ctrl+K,然后按Ctrl+D按顺序排列。
  • Format Selection (Ctrl+K,Ctrl+F)
  • 格式选择(Ctrl + K,Ctrl + F)

Toolbar Edit -> Advanced (If you can't see Advanced, select a code file in solution explorer and try again)

工具栏编辑->高级(如果您看不到高级,请在解决方案资源管理器中选择一个代码文件并再次尝试)

Your shortcuts might display differently to mine as I am set up for C# coding but navigating via the toolbar will get you to your ones.

当我为c#编码而设置时,你的快捷方式可能会和我的不同,但是通过工具栏导航会让你找到你的快捷键。

If it isn't working, look for errors in your code, like missing brackets which stop auto format from working

如果它不工作,在代码中查找错误,比如缺少了阻止自动格式工作的括号。

#3


32  

Follow the steps below:

遵循下面的步骤:

  • Go to Tools
  • 去工具
  • Go to Options
  • 去选择
  • Go to the Text Editor options
  • 切换到文本编辑器选项。
  • Click the language of your choice. I used C# as an example.
  • 点击你选择的语言。我以c#为例。

See the below image:

看到下面的图片:

如何在visualstudio中自动格式化代码?

#4


25  

Even though the question is a bit old, someone might find it handy.

尽管这个问题有点旧,但有人可能会发现它很方便。

You can define new key bindings by going to Tools -> options -> Environment -> keyboard

您可以通过使用工具->选项->环境->键盘来定义新的键绑定。

如何在visualstudio中自动格式化代码?

#5


15  

I have installed an extension named "Format document on Save" which formats the whole document every time you save it. For install it in VS15 or 17, on Tools just click the "Extensions and Updates...":

我已经安装了一个名为“保存格式文档”的扩展,每次保存它时,它都会格式化整个文档。在VS15或17中安装它,工具只需点击“扩展和更新…”

如何在visualstudio中自动格式化代码?

And then just go to "Online" at the left panel and search for "Format document on save" 如何在visualstudio中自动格式化代码?

然后在左边的面板上“在线”搜索“保存格式文档”

I hope it helps!.

我希望它可以帮助!

#6


9  

If you display the HTML Source Editing toolbar there is a "Format the Whole Document" button as well

如果你显示的是HTML源码编辑工具栏,也会有一个“整个文档的格式”按钮。

#7


8  

The solution provided in accepted answer does not apply to Microsoft Visual Studio 2012.

在接受的答案中提供的解决方案不适用于Microsoft Visual Studio 2012。

In case of VS2012 the shortcuts are:

在VS2012的情况下,捷径是:

  • For a highlighted block of code: Ctrl + K, Ctrl + F
  • 对于高亮显示的代码块:Ctrl + K, Ctrl + F。
  • For the document-wide formatting: Ctrl + K, Ctrl + D
  • 对于文档范围格式:Ctrl + K, Ctrl + D。

#8


7  

I used to use these combinations. I automated this process on Save of a document. You can try mine extension Format Document on Save

我曾经使用过这些组合。我在保存文档时自动执行此过程。你可以试试我的扩展格式文件。

#9


6  

  1. Go to Tools -> Extensions & Updates and type "productivity" in search: 如何在visualstudio中自动格式化代码?
  2. 去工具->扩展和更新和类型“生产力”在搜索:
  3. Install 'Productivity Power Tools 2015'
  4. 安装“2015年生产力工具”
  5. Restart VS.
  6. 重启VS。
  7. Go to Tools -> Options -> Productivity Power Tools -> Power Commands and check "Format document on save": 如何在visualstudio中自动格式化代码?
  8. 去工具->选项->生产力工具->电源命令和检查“格式文件保存”:

#10


5  

Under Under Tools -> Options -> Text Editor, then going to the Formatting -> General section of whatever language you wish to format you will find General. Check all three formatting check-boxes.

在下面的工具->选项->的文本编辑器,然后进入格式化->的任何语言你希望格式化你将找到通用。检查所有三种格式的复选框。

Under Tools -> Options -> Text Editor, then going to the TABS section of whatever language you wish to format you will find Indenting. Select Smart and it will activate automatic formatting whenever you use one of the closing elements ; ) } within that block.

在工具栏->选项->文本编辑器,然后进入你想要格式化的任何语言的选项卡部分,你会发现缩进。选择Smart,当您使用一个关闭元素时,它将激活自动格式;在那块}。

No need for keystrokes.

不需要按键。

#11


4  

You can add the buttons to your toolbar by clicking the little drop down arrow to the right of the last toolbar button, select "Add or Remove Buttons" and then click the buttons you want to add a tick to them. The button(s) you select will appear on your toolbar ...

您可以通过单击最后一个工具栏按钮右边的小箭头按钮,将按钮添加到工具栏中,选择“添加或删除按钮”,然后单击要向它们添加一个标记的按钮。你选择的按钮会出现在你的工具栏上。

如何在visualstudio中自动格式化代码?

Then you just select text and click the Increase Indent or Decrease Indent buttons. I tested this on Visual Studio 2013 only.

然后你只需选择文本并点击增加缩进或减少缩进按钮。我只在Visual Studio 2013上测试过。

#12


4  

Right click:

右键点击:

如何在visualstudio中自动格式化代码?

Works in VS 2015, maybe earlier version.

在VS 2015,可能更早的版本。

#13


4  

The original question said "I cannot find the setting."

最初的问题是:“我找不到布景。”

Simple answer is: Look at top menu, then

简单的回答是:看看上面的菜单。

Edit --> Advanced --> Format Document

编辑->高级->格式文件。

You will also see the currently assigned key strokes for that function. Nothing special to memorize. This really helps if you use multiple developer environments on different operating systems.

您还将看到该函数当前指定的键击。没有什么特别的记忆。如果在不同的操作系统上使用多个开发人员环境,这将非常有用。

#14


2  

In VS 2017 Format Document is CTRL E + D.

在VS 2017格式文件是CTRL + D。

But...if you want to add the Format Document button to a tool bar do this.
Right click on tool bar.
Select "Customize.."
Select the "Commands" Tab.
Select the "Toolbar" radio button.
Select "Text Editor" from the pull down next to the radio button (or what ever tool bar you want the botton on)

但是…如果您想要将格式文档按钮添加到工具栏中,请执行此操作。右键单击工具栏。选择“定制. .”选择“命令”选项卡。选择“工具栏”单选按钮。选择“文本编辑器”,从下拉到单选按钮(或者是你想要的工具栏)

Now...

现在…

Click the Add Command button.
Categories: Edit
Commands: Document Format
Click OK

单击Add命令按钮。类别:编辑命令:文档格式点击确定。

#15


1  

With the Continuous Formatting extension (commercial, developed by me), the code is formatted really automatically as you type.

随着持续格式化扩展(由我开发的商业模式),代码会随着您的类型自动地进行格式化。

#16


1  

Just to further Starwfanatic and Ewan's answers above. You can customise your IDE to add any button to any toolbar - so you can add the Format button (as the HTML Source Editing toolbar has) to any other toolbar (like Text Editing with all the other edit controls like increase/decrease indent).

只是为了进一步了解Starwfanatic和Ewan的答案。您可以定制您的IDE,以向任何工具栏添加任何按钮——因此您可以将Format按钮(作为HTML源代码编辑工具栏)添加到任何其他工具栏(如文本编辑和其他所有编辑控件,如增加/减少缩进)。

Click the arrow to the right of the toolbar > Add or Remove Buttons > Customize... > Commands tab > Add Command... button.

单击工具栏>右边的箭头,添加或删除按钮>自定义…>命令tab >添加命令…按钮。

Document Format and Selection Format are both under the Edit group.

文档格式和选择格式都在编辑组中。

(Tested in VS2010 and VS2013)

(VS2010和VS2013测试)

#17


1  

Cut/Paste of a section is another quick way (and easy to remember).

剪切/粘贴一个部分是另一个快速的方法(而且容易记住)。

#18


1  

In newer versions, the shortcut for the document-wide formatting is: Shift + Alt + F

在较新的版本中,文档范围格式的快捷方式是:Shift + Alt + F。

#19


0  

If you can afford it (or if you're eligible for the 30-day free trial) Jetbrains ReSharper can reformat a whole project directory.

如果你能负担得起(或者如果你有资格享受30天免费试用),Jetbrains ReSharper可以重新格式化整个项目目录。

Just install -> Right-click a directory -> select Cleanup Code from the context menu.

只需安装->右键单击一个目录->从上下文菜单中选择清除代码。

#20


0  

In Visual Studio 2015 and 2017 for c# code. 1) Scroll to the end of the file 2) Remove the last "Curly Bracket" } 3) Wait until the line above it shows an error 4) Replace the "Curly Bracket" } fini. :)

在Visual Studio 2015和2017的c#代码。1)滚动到文件的末尾,删除最后一个“花括号”,直到上面的行显示一个错误4)替换“花括号”}fini。:)