1867

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

1

22 Answers 22

3372

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

16
  • 156
    You can also right click in the editor an select: Outlining -> Toggle All Outlining
    – LBushkin
    Jun 11, 2009 at 18:11
  • 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. Jan 13, 2010 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, 2011 at 11:30
  • 15
    For what it's worth, these work in Visual Studio 2012 as well Nov 1, 2012 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, 2014 at 7:56
203

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.

3
  • 3
    Ctrl-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, 2014 at 6:35
  • Very nice idea to remember easily.
    – Koray
    Sep 20, 2019 at 13:44
  • how can you have "ctrl-M-M" ?? There is only one M key.
    – Nerdy Bunz
    Dec 26, 2022 at 3:29
119

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.

2
  • 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. Jan 13, 2010 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. Aug 31, 2012 at 18:27
44

CTRL+M expands region.

CTRL+L collapses region.

2
  • But it is only useful command when using js outlining extension, and I have 1000+ lines js file, this shortcut is a** saver! Oct 21, 2011 at 8: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, 2012 at 5: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
0
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"

0
28

CTRL + M + A collapses all

works for me, whereas

CTRL + M + O

does not

25

Press

CTRL + A

Then

CTRL + M + M

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

1
18

In Visual Studio 2013:

CTRL + M + A collapses all

CTRL + M + L expands all

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

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

14

Are you refering to the toggle outlining?

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

4

To collapse all use:

Ctrl + M and Ctrl+A

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

0
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 + [.

1
  • Thanks. This is probably the latest shortcut that works
    – codetinker
    Feb 1, 2018 at 3:10
3

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

enter image description here

3

Collapse All - Ctrl + M + O

Expand All - Ctrl + M + L

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

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.

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.

0

In short, through "Tools … Settings":

In short through tools settings

1
  • 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.
    – Community Bot
    Oct 1, 2021 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.

-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

1
  • 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, 2020 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.