Visual Studio 扩展/折叠键盘快捷键

225
在 Visual Studio 中,如果我打开了一个代码文件,我可以按下 CTRL + MCTRL + M + O 来折叠所有的代码块、区域、命名空间等。
那么如何展开所有内容呢?
我已经搜索了一下,但好像找不到适用的快捷键!
7个回答

379

折叠到定义

CTRL + M, O

展开所有大纲

CTRL + M, X

展开或折叠所有内容

CTRL + M, L

这也适用于其他语言,例如TypeScript和JavaScript


33
我用谷歌搜索并得到了你的答案;) - Bartosz
10
不要同时按下三个按钮!先按住CTRL键,然后按M键,再按O键或X键。;-) - Krisztián Balla
当我在vs2017中执行此操作时,我收到了“键组合(Ctrl + M,O)不是命令”的消息。实际上是(Ctrl + M,Ctrl + O),通过进入环境设置找到的。我不确定在vs2017中是否有所改变。 - Dan Chase
@DanChase 你有安装任何可能覆盖键映射的插件吗?例如Resharper之类的东西?(并不是在暗示Resharper会这样做,但我也不会感到惊讶...) - Matthew Layton
@DanChase 顺便说一下,我已经安装了VS2017企业版,并且在这里可以正常工作。 - Matthew Layton
显示剩余5条评论

139

如您所见,有几种方法可以实现这一点。

我个人使用:

全部展开:CTRL + M + L

全部折叠:CTRL + M + O

额外奖励:

在光标位置展开/折叠:CTRL + M + M


7
毋须重复前面的评论,但 Ctrl M M 真是太棒了,可以为我节省很多时间! - Ciaran Martin
1
关于位置展开折叠的非常有用的奖励 - MemeDeveloper
2
CMM +1 网络饼干送给你。比我想要的更好! - WernerCD
1
可以确认在Visual Studio 2019中,Ctrl+M+M是有效的。 - Seb
2
@Allstar 只需再次按下 CTRL+M+M 即可 :-) - Sirar Salih
显示剩余3条评论

33

Visual Studio 2015:

Tools > Options > Settings > Environment > Keyboard

默认值:

Edit.CollapsetoDefinitions: CTRL + M + O

Edit.CollapseCurrentRegion: CTRL + M +CTRL + S

Edit.ExpandAllOutlining: CTRL + M + CTRL + X

Edit.ExpandCurrentRegion: CTRL + M + CTRL + E

我喜欢设置和使用IntelliJ的快捷键:

Edit.CollapsetoDefinitions: CTRL + SHIFT + NUM-

Edit.CollapseCurrentRegion: CTRL + NUM-

Edit.ExpandAllOutlining: CTRL + SHIFT + NUM+

Edit.ExpandCurrentRegion: CTRL + NUM+


1
CME就是我想要的。给你一个网络Cookie。 - WernerCD

22
您可以使用 Ctrl + MCtrl + P
这被称为 Edit.StopOutlining。

1
谢谢!...我很想知道你是如何将这些快捷方式格式化成键盘按键的样式! - Matthew Layton
@series0ne 已经用 kbd 标签包装它们 :) - Ufuk Hacıoğulları
3
如果您先按下Ctrl+M,则不会打印出来。 - Ufuk Hacıoğulları

13

如果要折叠代码块,可以尝试使用 CTRL + M + O 快捷键,展开则使用 CTRL + M + P。这在 VS2008 中有效。


这是 CTRL + M + O - 字母 O,不是数字 0。清理编辑引入了错误到答案中。 - goodeye

8

请前往 工具->选项->文本编辑器->c#->高级,取消勾选第一个复选框“打开文件时进入大纲模式”。

这将永久解决此问题。


终于有人在思考了,非常感谢,我会将您的评论标记为真正的答案。 - pixel
请注意,此设置使得在打开代码文件时所有区域都自动展开 - 对我来说,这比使用键盘快捷键更好,因为我喜欢永远不看到折叠的区域。 - jrh

5

我一直希望Visual Studio有一个选项可以只折叠/展开区域。我有以下宏可以实现这一点。

Imports EnvDTE
Imports System.Diagnostics
' Macros for improving keyboard support for "#region ... #endregion"
Public Module CollapseExpandRegions
' Expands all regions in the current document
  Sub ExpandAllRegions()

    Dim objSelection As TextSelection ' Our selection object

    DTE.SuppressUI = True ' Disable UI while we do this
    objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
    objSelection.StartOfDocument() ' Shoot to the start of the document

    ' Loop through the document finding all instances of #region. This action has the side benefit
    ' of actually zooming us to the text in question when it is found and ALSO expanding it since it
    ' is an outline.
    Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
        ' This next command would be what we would normally do *IF* the find operation didn't do it for us.
        'DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
    Loop
    objSelection.StartOfDocument() ' Shoot us back to the start of the document
    DTE.SuppressUI = False ' Reenable the UI

    objSelection = Nothing ' Release our object

  End Sub

  ' Collapses all regions in the current document
  Sub CollapseAllRegions()
    Dim objSelection As TextSelection ' Our selection object

    ExpandAllRegions() ' Force the expansion of all regions

    DTE.SuppressUI = True ' Disable UI while we do this
    objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
    objSelection.EndOfDocument() ' Shoot to the end of the document

    ' Find the first occurence of #region from the end of the document to the start of the document. Note:
    ' Note: Once a #region is "collapsed" .FindText only sees it's "textual descriptor" unless
    ' vsFindOptions.vsFindOptionsMatchInHiddenText is specified. So when a #region "My Class" is collapsed,
    ' .FindText would subsequently see the text 'My Class' instead of '#region "My Class"' for the subsequent
    ' passes and skip any regions already collapsed.
    Do While (objSelection.FindText("#region", vsFindOptions.vsFindOptionsBackwards))
        DTE.ExecuteCommand("Edit.ToggleOutliningExpansion") ' Collapse this #region
        'objSelection.EndOfDocument() ' Shoot back to the end of the document for
        ' another pass.
    Loop
    objSelection.StartOfDocument() ' All done, head back to the start of the doc
    DTE.SuppressUI = False ' Reenable the UI

    objSelection = Nothing ' Release our object

  End Sub
End Module

编辑:现在有一个名为Edit.ToggleOutliningExpansion(Ctrl+M,Ctrl+M)的快捷方式,可以完成这个操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接