Command to collapse all sections of code?

Visual Studio

Visual Studio Problem Overview


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

Visual Studio Solutions


Solution 1 - Visual Studio

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

Solution 2 - Visual Studio

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.

Solution 3 - Visual Studio

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.

Solution 4 - Visual Studio

CTRL+M expands region.

CTRL+L collapses region.

Solution 5 - Visual Studio

  • 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

Solution 6 - Visual Studio

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"

Solution 7 - Visual Studio

CTRL + M + A collapses all

works for me, whereas

CTRL + M + O

does not

Solution 8 - Visual Studio

Press

CTRL + A

Then

CTRL + M + M

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

Solution 9 - Visual Studio

In Visual Studio 2013:

CTRL + M + A collapses all

CTRL + M + L expands all

Solution 10 - Visual Studio

Are you refering to the toggle outlining?

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

Solution 11 - Visual Studio

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/

Solution 12 - Visual Studio

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

Solution 13 - Visual Studio

To collapse all use:

Ctrl + M and Ctrl+A

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

Solution 14 - Visual Studio

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

Solution 15 - Visual Studio

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

enter image description here

Solution 16 - Visual Studio

Collapse All - Ctrl + M + O

Expand All - Ctrl + M + L

Solution 17 - Visual Studio

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.

Solution 18 - Visual Studio

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.

Solution 19 - Visual Studio

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

Solution 20 - Visual Studio

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.

Solution 21 - Visual Studio

In short, through "Tools … Settings":

In short through tools settings

Solution 22 - Visual Studio

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMr. FlibbleView Question on Stackoverflow
Solution 1 - Visual StudioBobView Answer on Stackoverflow
Solution 2 - Visual StudioBhavesh KachhadiyaView Answer on Stackoverflow
Solution 3 - Visual StudioScott IveyView Answer on Stackoverflow
Solution 4 - Visual StudioM4NView Answer on Stackoverflow
Solution 5 - Visual StudioAvatarOfChronosView Answer on Stackoverflow
Solution 6 - Visual StudioGelásioView Answer on Stackoverflow
Solution 7 - Visual StudioRayView Answer on Stackoverflow
Solution 8 - Visual StudioCarlView Answer on Stackoverflow
Solution 9 - Visual StudioNathan ChaseView Answer on Stackoverflow
Solution 10 - Visual StudioCodeLikeBeakerView Answer on Stackoverflow
Solution 11 - Visual StudioShobanView Answer on Stackoverflow
Solution 12 - Visual StudioHarishhView Answer on Stackoverflow
Solution 13 - Visual StudioAhmadView Answer on Stackoverflow
Solution 14 - Visual StudiomclhrnView Answer on Stackoverflow
Solution 15 - Visual StudioJuned AnsariView Answer on Stackoverflow
Solution 16 - Visual StudioVikas ChaturvediView Answer on Stackoverflow
Solution 17 - Visual StudioAbdulhameedView Answer on Stackoverflow
Solution 18 - Visual StudioDavidRRView Answer on Stackoverflow
Solution 19 - Visual StudioNeel DsouzaView Answer on Stackoverflow
Solution 20 - Visual StudioAndrewView Answer on Stackoverflow
Solution 21 - Visual Studiokapil kaushikView Answer on Stackoverflow
Solution 22 - Visual StudioRajkumar SView Answer on Stackoverflow