Visual Studio – Collapse and Expand Shortcuts – What they actually do

Visual Studio has several shortcuts available for expanding and collapsing sections of code. These Shortcuts have been tested with Visual Studio 2017 15.87 with default settings. Below I explain what each of these does in more detail.

Collapse All Outlining (Edit.CollapseAllOutlining)
Ctrl+ (M, A)

Toggle All Outlining (Edit.ToggleAllOutlining)
Ctrl+ (M, L)

Toggle Outline Expansion (Edit.ToggleOutlinineExpansion)
Ctrl+ (M,M)

Expand All Outlining (Edit.ExpandAllOutlining)
Ctrl+ (M,X)

Expand Current Region (Edit.ExpandCurrentRegion)
Ctrl+(M,E)

Collapse Current Region (Edit.CollapseCurrentRegion)
Ctrl+(M,S)

Toggle Outline Expansion
Ctrl+ (M,M) – This is probably the shortcut that makes the most sense of this bunch. Pressing this command repeatedly will open and close the region or auto-outlined section under the cursor. 

In the example below this can be used to open and close the constructors region, as well as the Foo and Bar2 types separately.

Collapse All Outlining
Ctrl+ (M, A) In the code below, this command does what you would expect. All outlining is collapsed as much as possible. 

Expand All Outlining
Ctrl+ (M,X) – this command works great for expanding everything so all code is visible

Toggle All Outlining
Ctrl+ (M,L)  this command behaves someone strangely. The word toggle suggests that repeating this command will switch outlining back and forth. This isn’t quite what happens. The first time you press Ctrl+ (M,L) if there is a mixture of open and closed sections in the file, the closed sections will be opened as the first step in the toggle. 

In the example below, if the Constructors region starts closed, typing this command will first open the region, and the command must be executed a second time to actually toggle all outlining. This is relatively unintuitive. 


Expand Current Region and Collapse Current Region
Ctrl + (M,E) – Expand
Ctrl + (M,S) – Collapse

These commands will collapse or expand the region under the cursor. While editing code I have a hard time thinking of time where one of these commands would be preferable to “Toggle Outline Expansion” above. 

The associated commands may be useful outside of an editing context, such as in auto generated code, code assistance extensions, etc.


Below is simple file to demonstrate the outlining system…

namespace ExpandCollapse
{


    public class Foo
    {
        public int Integer1 { get; set; }

        #region Constructors 
        public Foo()
        {

        }
        #endregion

    }

    public class Bar2
    {

        public Bar2()
        {
            Console.WriteLine("Hello from Bar2");
        }

        public int Int1 { get; set; }
    }

    public class Food
    {

    }
}