1803

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

Dark Matter
  • 1,807
  • 2
  • 25
  • 32
Mr. Flibble
  • 25,865
  • 21
  • 68
  • 98

22 Answers22

3255

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
  • 23
  • 9
Bob
  • 93,968
  • 29
  • 116
  • 128
  • 151
    You can also right click in the editor an select: Outlining -> Toggle All Outlining – LBushkin Jun 11 '09 at 18:11
  • I just gave L a try, that works too, thanks, I'll update my post – Bob Jun 11 '09 at 18:18
  • I love CTRL +M +P We have an id**t that nests regions 4 deep. – Jim Evans Jun 12 '09 at 15:37
  • 26
    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
  • 22
    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
  • 14
    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. – Gökberk Konuralp 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
189

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,381
  • 2
  • 13
  • 26
Bhavesh Kachhadiya
  • 3,752
  • 3
  • 14
  • 20
113

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
  • 2,801
  • 2
  • 24
  • 37
Scott Ivey
  • 39,608
  • 21
  • 79
  • 117
  • 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
42

CTRL+M expands region.

CTRL+L collapses region.

Josh Correia
  • 2,801
  • 2
  • 24
  • 37
M4N
  • 92,565
  • 45
  • 215
  • 258
  • 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
34
  • 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
  • 857
  • 9
  • 24
28

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,869
  • 1
  • 19
  • 17
24

CTRL + M + A collapses all

works for me, whereas

CTRL + M + O

does not

Josh Correia
  • 2,801
  • 2
  • 24
  • 37
Ray
  • 448
  • 3
  • 11
16

Press

CTRL + A

Then

CTRL + M + M

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

Josh Correia
  • 2,801
  • 2
  • 24
  • 37
Carl
  • 161
  • 1
  • 3
15

In Visual Studio 2013:

CTRL + M + A collapses all

CTRL + M + L expands all

Josh Correia
  • 2,801
  • 2
  • 24
  • 37
Nathan Chase
  • 1,941
  • 2
  • 17
  • 15
14

Are you refering to the toggle outlining?

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

lime
  • 6,553
  • 3
  • 37
  • 50
CodeLikeBeaker
  • 19,633
  • 13
  • 75
  • 105
13

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
  • 35,375
  • 44
  • 163
  • 278
Shoban
  • 22,815
  • 8
  • 62
  • 106
12

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

xameeramir
  • 25,752
  • 22
  • 132
  • 205
Harishh
  • 129
  • 1
  • 3
4

To collapse all use:

Ctrl + M and Ctrl+A

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

xameeramir
  • 25,752
  • 22
  • 132
  • 205
Ahmad
  • 1,372
  • 13
  • 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,821
  • 9
  • 11
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
  • 4,684
  • 4
  • 49
  • 82
3

Collapse All - Ctrl + M + O

Expand All - Ctrl + M + L

Agilanbu
  • 2,615
  • 2
  • 27
  • 31
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
  • 301
  • 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
  • 16,676
  • 19
  • 101
  • 178
1

For Visual Studio 2021 update.

I've been through all the answers but none worked for me for this update.

So posting a simple way to find out the shortcut as this shortcuts vary for different version. Go to HelpKeyboard Shortcut Preferences (Ctrl + K Ctrl + R) It will take you to this pdf which contains all the shortcuts. So search for your desired shortcut that is fold/collapse and use that in your function.

CTRL+Shift+[ → Fold (collapse) region

CTRL+Shift+] → Unfold (uncollapse) region

CTRL+K CTRL+[ → Fold (collapse) all subregions

CTRL+K CTRL+] → Unfold (uncollapse) all subregions

CTRL+K CTRL+0 → Fold (collapse) all regions

CTRL+K CTRL+J → Unfold (uncollapse) all regions

Once you get the shortcut key, You can go to the Keyboard Shortcuts (Ctrl + K Ctrl + S) and search for the shortcut key and replace it with your desired key.

Neel Dsouza
  • 1,138
  • 2
  • 11
  • 26
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
  • 16,273
  • 10
  • 93
  • 104
0

In short, through "Tools … Settings":

In short through tools settings

Adrian Mole
  • 43,040
  • 110
  • 45
  • 72
  • 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
-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,615
  • 2
  • 27
  • 31
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