In Visual Studio, is there a command to collapse/expand all the sections of code in a file?
-
3If 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 Answers
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.
-
156You can also right click in the editor an select: Outlining -> Toggle All Outlining – LBushkin Jun 11 '09 at 18:11
-
-
27ctrl+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
-
24Just 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
-
5In VisualStudio 2013 ctrl+M+L toggles collapse state, ctrol+M+O doesn't do anything (HTML VIEW) – Ayyash Mar 18 '14 at 07:56
-
2Collapse: `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
-
1In 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
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.

- 1,467
- 2
- 14
- 27

- 3,902
- 3
- 15
- 20
-
3Ctrl-M,L is Toggle all, but definitely the Ctrl-M,A and Ctrl-M,X are the ones I was looking for... – sharpener Jul 29 '14 at 06:35
-
-
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.

- 3,807
- 3
- 33
- 50

- 40,768
- 21
- 80
- 118
-
5As 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
CTRL+M expands region.
CTRL+L collapses region.

- 3,807
- 3
- 33
- 50

- 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
- 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

- 867
- 9
- 24
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"

- 1,899
- 1
- 19
- 18
CTRL + M + A collapses all
works for me, whereas
CTRL + M + O
does not

- 3,807
- 3
- 33
- 50

- 488
- 4
- 11
Press
CTRL + A
Then
CTRL + M + M
To compress all, including child nodes, in XML-files.

- 3,807
- 3
- 33
- 50

- 369
- 3
- 3
In Visual Studio 2013:
CTRL + M + A collapses all
CTRL + M + L expands all

- 3,807
- 3
- 33
- 50

- 2,071
- 2
- 18
- 15
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/

- 39,871
- 52
- 185
- 313

- 22,920
- 8
- 63
- 107
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

- 28,977
- 24
- 140
- 219

- 149
- 1
- 3
Are you refering to the toggle outlining?
You can do: Control + M then Control + L to toggle all outlining

- 6,901
- 4
- 39
- 50

- 20,682
- 14
- 79
- 108
To collapse all use:
Ctrl + M and Ctrl+A
All shortcuts for VS 2012/2013/2015 available at http://visualstudioshortcuts.com/2013/

- 28,977
- 24
- 140
- 219

- 1,462
- 15
- 23
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 + [.

- 1,839
- 11
- 13

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

- 5,035
- 7
- 56
- 89
Collapse All - Ctrl + M + O
Expand All - Ctrl + M + L

- 2,747
- 2
- 28
- 33

- 477
- 4
- 7
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.

- 322
- 3
- 13
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.

- 18,291
- 25
- 109
- 191
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.

- 18,680
- 13
- 103
- 118
In short, through "Tools … Settings":

- 49,934
- 160
- 51
- 83
-
1Your 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
For outlining options: Go to Tools -> Options -> Text Editor -> C# -> Advanced -> Outlining for outlining options.
There's an option for regions.

- 11
- 3
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

- 2,747
- 2
- 28
- 33

- 94
- 3
-
1This 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