Command to collapse all sections of code?

Viewed 868491

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

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.

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

CTRL+M expands region.

CTRL+L collapses region.

  • 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

In Visual Studio 2013:

CTRL + M + A collapses all

CTRL + M + L expands all

Are you refering to the toggle outlining?

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

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

Collapse All - Ctrl + M + O

Expand All - Ctrl + M + L

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.

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.

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.

In short, through "Tools … Settings":

In short through tools settings

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

Related