1861

In Visual Studio, is there a command to collapse/expand all the sections of code in a file?

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Mr. Flibble
  • 26,564
  • 23
  • 69
  • 100
  • 3
    If anyone still happens to look for this info (13 years later, in 2022), please visit [https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf) - and try out Ctrl+K Ctrl+0 (this is the digit 0/zero) to "fold" (ie, collapse) all. Unfold using Ctrl+K Ctrl+J. – jsN00b Aug 08 '22 at 16:22

22 Answers22

3364

CTRL + M + O will collapse all.

CTRL + M + L will expand all. (in VS 2013 - Toggle All outlining)

CTRL + M + P will expand all and disable outlining.

CTRL + M + M will collapse/expand the current section.

CTRL + M + A will collapse all even in Html files.

These controls are also in the context menu under Outlining.

Right click in editor -> Outlining to find these controls. (After disabling outlining, use same steps to enable outlining.)

For outlining options: Go to Tools -> Options -> Text Editor -> C# -> Advanced -> Outlining for outlining options.

Tools -> Options -> Text Editor -> C# -> Advanced -> Outlining Right-click > Outlining menu

Krist0f
  • 63
  • 9
Bob
  • 97,670
  • 29
  • 122
  • 130
  • 156
    You can also right click in the editor an select: Outlining -> Toggle All Outlining – LBushkin Jun 11 '09 at 18:11
  • I love CTRL +M +P We have an id**t that nests regions 4 deep. – Jim Evans Jun 12 '09 at 15:37
  • 27
    ctrl+m ctrl+o is "collapse to definitions" which doesn't work when editting an xml file. For me ctrl+m ctrl+l expands all and collapses all (as toggle), but not always. I have one file open where it works and one where it only expands all, but not collapse all. Both are xml files. No idea why. – Matthijs Wessels Jan 13 '10 at 13:07
  • 24
    Just to inform (though this is pretty old): ctrl + m + o only collapses all FUNCTIONS, not the classes (or regions etc). ctrl +m + L will toggle expanding, collapsing everything. – paul23 Oct 11 '11 at 11:30
  • Ctrl + M + M works on selected text too. For instance, in a completely collapsed xml document with your cursor on an element Ctrl + M + M will expand that node but not its children. If you highlight through the closing tag Ctrl + M + M will expand all of that element's children too. – phloopy Nov 15 '11 at 21:57
  • 15
    For what it's worth, these work in Visual Studio 2012 as well – wjhguitarman Nov 01 '12 at 19:14
  • 5
    In VisualStudio 2013 ctrl+M+L toggles collapse state, ctrol+M+O doesn't do anything (HTML VIEW) – Ayyash Mar 18 '14 at 07:56
  • 2
    Collapse: `Magic -> Obfuscate` and Expand: `Magic -> Look` Just a helpful acronym to help me remember the hotkeys... It's the best I could come up with, but it works! – Levi Fuller Aug 17 '15 at 22:45
  • One specific method when I collapse all stays expanded, anybody know why? – John Demetriou Nov 11 '15 at 10:07
  • Thx, why this is the first place i saw CTRL+M + M. Just an extra note, Depending on what is highlighted, is what it will open, Function Name - just function - nothing thats collapsed inside of. Highlight all function code - will open and collapse all the highlighted text. – Yogurt The Wise Dec 08 '15 at 21:09
  • 1
    In Visual Studio 2017, collapsing regions via shortcut is disabled by default. To enable, check Tools > Options > Text Editors > c#> Advanced > Outlining > Collapse #regions when collapsing to definitions. – gkonuralp Sep 22 '17 at 11:29
  • 1
    @gkon - They also decide to change it to Ctrl + M + Ctrl + L (extra ctrl) ugh – SwDevMan81 Jan 18 '18 at 15:18
  • VS2017 Community Edition , C++ code ... almost works. Lambdas are ignored. But. CTRL+M+M works as advertised. –  Mar 08 '18 at 19:03
  • Highlite a section of code with within a With xxx End With and hit CNTRL + M then CNTRL + H and it will add its own special code collapse – Argyle Ghost Apr 23 '18 at 11:48
  • works in vs community 2022. Also huge thanks.... i have always wanted regions to collapse. To date I had not noticed the 'Collapse Regions' checkbox. Soooo nice. – greg Jun 20 '22 at 14:43
  • to make this extra clear; when everyone say ctrl+m+something else it is actually ctrl+m and then press that key. Example: Ctrl+M then O. This happens in Visual Studio 2022 for C#. I'm not sure why it would be different for anything else, but just in case. – user2455808 Jun 27 '23 at 16:41
202

The following key combinations are used to do things:

CTRL + M + M → Collapse / Expand current preset area (e.g. Method)

CTRL + M + H → Collapse / Hide (Expand) current selection

CTRL + M + O → Collapse all(Collapse declaration bodies)

CTRL + M + A → Collapse all

CTRL + M + X → Expand all

CTRL + M + L → Toggle all

There some options in the context menu under Outlining.

Peter Ivan
  • 1,467
  • 2
  • 14
  • 27
Bhavesh Kachhadiya
  • 3,902
  • 3
  • 15
  • 20
118

CTRL + M + L expands all

CTRL + M + O collapses all

In the MSDN article "Default Keyboard Shortcuts in Visual Studio," see the section "Text Editor" if you're looking for other shortcuts - these 2 are just a few of the many that are available.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Scott Ivey
  • 40,768
  • 21
  • 80
  • 118
  • 5
    As I commented in the top answer, ctl+m+o collapses to definition which doesn't work for xml files. ctl+m+l should toggle it, but for me it sometimes does and sometimes doesn't. – Matthijs Wessels Jan 13 '10 at 13:10
  • Normal pages the crtl+m+o collapse, the JScript outling is backwards, for mine, crtl+m+o expands, and the crtl+M+l expands. – Yogurt The Wise Aug 31 '12 at 18:27
43

CTRL+M expands region.

CTRL+L collapses region.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
M4N
  • 94,805
  • 45
  • 217
  • 260
  • But it is only useful command when using js outlining extension, and I have 1000+ lines js file, this shortcut is a** saver! – Goran Obradovic Oct 21 '11 at 08:53
  • AND WORKS FOR XML! WOOO HOOO!! (+1 purely for technical reasons; i'd gladly pay you some rep :) This is going into my regular cheatsheet – Gishu Sep 12 '12 at 05:56
35
  • Fold/Unfold the current code block – Ctrl+M, Ctrl+M
  • Unfold all – Ctrl+M, Ctrl+L
  • Stop outlining – Ctrl+M, Ctrl+P
  • Fold all – Ctrl+M, Ctrl+O
AvatarOfChronos
  • 867
  • 9
  • 24
29

In Visual Studio 2017, It seems that this behavior is turned off by default. It can be enabled under Tools > Options > Text Editors > C# > Advanced > Outlining > "Collapse #regions when collapsing to definitions"

Gelásio
  • 1,899
  • 1
  • 19
  • 18
28

CTRL + M + A collapses all

works for me, whereas

CTRL + M + O

does not

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Ray
  • 488
  • 4
  • 11
25

Press

CTRL + A

Then

CTRL + M + M

To compress all, including child nodes, in XML-files.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Carl
  • 369
  • 3
  • 3
18

In Visual Studio 2013:

CTRL + M + A collapses all

CTRL + M + L expands all

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Nathan Chase
  • 2,071
  • 2
  • 18
  • 15
16

If you mean shortcut then

CTRL + M + M: This one will collapse the region your cursor is at whether its a method, namespace or whatever for collapsing code blocks, regions and methods. The first will collapse only the block/method or region your cursor is at while the second will collapse the entire region you are at.

http://www.dev102.com/2008/05/06/11-more-visual-studio-shortcuts-you-should-know/

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
Shoban
  • 22,920
  • 8
  • 63
  • 107
14

Below are all what you want:

  • Collapse / Expand current Method

CTRL + M + M

  • Collapse / Expand current selection

CTRL + M + H

  • Collapse all

CTRL + M + O

CTRL + M + A

  • Expand all

CTRL + M + X

CTRL + M + L

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
Harishh
  • 149
  • 1
  • 3
14

Are you refering to the toggle outlining?

You can do: Control + M then Control + L to toggle all outlining

lime
  • 6,901
  • 4
  • 39
  • 50
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
4

To collapse all use:

Ctrl + M and Ctrl+A

All shortcuts for VS 2012/2013/2015 available at http://visualstudioshortcuts.com/2013/

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
Ahmad
  • 1,462
  • 15
  • 23
4

None of these worked for me. What I found was, in the editor, search the Keyboard Shortcuts file for editor.foldRecursively. That will give you the latest binding. In my case it was CMD + K, CMD + [.

Nipun Wijerathne
  • 1,839
  • 11
  • 13
mclhrn
  • 145
  • 11
3

if you want to collapse and expand particular loop, if else then install following plugins for visual studio.

enter image description here

Juned Ansari
  • 5,035
  • 7
  • 56
  • 89
3

Collapse All - Ctrl + M + O

Expand All - Ctrl + M + L

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
2

In case of ugrading to Visual Studio 2010, 2012, 2013 or 2015, there's a Visual Studio extension to show current registered keyboard shortcuts, IntelliCommand.

Abdulhameed
  • 322
  • 3
  • 13
2

Visual Studio can help you find the answer to your question in a couple of different ways.

Type Ctrl + Q to access Quick Launch, and then enter "collap". Quick Launch will display something like:

Menus (1)
    Edit -> Outlining -> Collapse to Definitions (Ctrl+M, Ctrl+O)
NuGet Packages (1)
    Search Online for NuGet Packages matching 'collap'

From there, enter "outlining" to find other menu entries related to outlining:

Menus (5)
    Edit -> Outlining -> Toggle Outlining Expansion (Ctrl+M, Ctrl+M)
    Edit -> Outlining -> Toggle All Outlining (Ctrl+M, Ctrl+L)
    Edit -> Outlining -> Stop Outlining (Ctrl+M, Ctrl+P)
    Edit -> Outlining -> Stop Hiding Current (Ctrl+M, Ctrl+U)
    Edit -> Outlining -> Collapse to Definitions (Ctrl+M, Ctrl+O)
Options (4)
    Text Editor -> Basic -> VB Specific (Change outline mode, Automatic inser...
    ...

However, note that Quick Launch will show you only those commands that are available as Visual Studio menu entries. To find other keyboard-related commands related to collapsing sections of code, in the Visual Studio menu bar click:

Tools -> Options -> Environment -> Keyboard

This will display the keyboard section in the Options dialog box. In the "Show commands containing" text box, enter "edit.collap". Visual Studio will display a list that is something like:

Edit.CollapseAllincurrentblock    
Edit.CollapseAllOutlining           Ctrl+M, Ctrl+A (Text Editor)
Edit.CollapseBlockcurrentblock
Edit.CollapseCurrentRegion          Ctrl+M, Ctrl+S (Text Editor)
Edit.CollapseTag                    Ctrl+M, Ctrl+T (Text Editor)
Edit.CollapsetoDefinitions          Ctrl+M, Ctrl+O (Text Editor)

You'll need to click each command to see its associated keyboard shortcut.


My examples taken from Visual Studio 2013.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
0

In Visual Studio 2019:

Go to Tools > Options > Keyboard.

Search for Edit.ToggleAllOutlining

Use the shortcut listed there, or assign it the shortcut of choice.

Andrew
  • 18,680
  • 13
  • 103
  • 118
0

In short, through "Tools … Settings":

In short through tools settings

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 01 '21 at 16:26
0

For outlining options: Go to Tools -> Options -> Text Editor -> C# -> Advanced -> Outlining for outlining options.

There's an option for regions.

Steve Tses
  • 11
  • 3
-1

If you want to collapse/expand an area within a class/method (instead of collapsing the entire class/method), you may create custom regions as follow:

 #region AnyNameforCollapsableRegion

 //Code to collapse

 #endregion 

Reference

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
Rajkumar S
  • 94
  • 3
  • 1
    This is not at all what regions are for. Regions are for maintainability and readability in code and should be used to group like methods, classes, etc. – Bmize729 Feb 28 '20 at 05:54