Collapsing Your Code with Outlining


Keyboard:  CTRL + M, CTRL + M
Menu:  Edit -> Outlining -> Toggle Outlining Expansion
Command:  Edit.ToggleOutliningExpansion
Versions:  2008,2010
Published:  3/15/2010
Code:  vstipEdit0029

 

By default, Outlining is enabled in Visual Studio.  It’s the line you see with the boxes to indicate the status of the area (collapsed or expanded):

image

 

You can collapse areas of code to get them out of your way so you can focus on other areas.  There are four ways to do it:

  1. Click on the minus sign to collapse an area of code:

    image

    NOTE:  In VS2010 it now highlights the area that will be collapsed as seen here.  A very cool feature.  If you don’t like the highlighting color you can go to Tools -> Options -> Environment -> Fonts and Colors -> Collapsible Region to change it.

  2. VS2010 ONLY:  Click ANYWHERE on the vertical line in the highlighted region.  In other words, you can now collapse a region from anywhere in that region:
    image 
  3. Click anywhere in the area to be collapsed and hit CTRL + M, CTRL + M
  4. Click anywhere in the area to be collapsed and go to Edit -> Outlining -> Toggle Outlining Expansion on the Menu Bar

 

Once collapsed, the code area will look like this:

image

Comments (16)

  1. Catto says:

    Hey Now Zain,

    Nice one, also after collapsing by Ctrl + M, Ctrl + O on a code behind file a nice one to know is how to:

    expand Ctrl + M, Ctrl + P

    Also combining these with #region’s are useful too.

    Thx 4 the info,

    Catto

  2. Upgrading from VisStudio 2005 says:

    Super. Now how do I turn this feature off? I could turn it off in VS 2005.

  3. zainnab says:

    There was a "feature" in older versions that would take out outlining when you removed the selection margin from the options.  In 2010, that doesn't exist so you have to remove it on a per-document basis.  You can use CTRL+M, CTRL+P ( which is Edit | Outlining | Stop Outlining on the menu bar).  To turn it off.

    Z

  4. Ed says:

    After doing Ctrl+M Ctrl+O, which collapses all, if I switch to a different document tab and switch back, everything auto-expands again. I want it to stay collapsed. Is there a way to keep it from auto-expanding every time I switch pages ?

  5. Rui says:

    it would be much better to collapse individual scopes (eg.: collapsing a switch statement)

  6. zainnab says:

    Rui – while it doesn't do it automatically, you CAN hide individual scopes yourself with hide selection:  blogs.msdn.com/…/using-hide-selection-vstipedit0036.aspx

    Ed – That is not normal behavior if it persists that might indicate a problem with the SUO file.

    Z

  7. Rei says:

    I have a problem with 2010 outlining,,

    in 2008 it used to collapse the newline too but in 2010 it leaves a space in between each collapsed segments. that's pretty annoying in terms of readability and makes the collapsed page longer than it should.

    Does anyone know how to fix it?

  8. zainnab says:

    Rei :)

    There is no way that I am aware of to reduce the space between collapsed regions.

    Z

  9. Marc Roussel says:

    How to keep comments on multiple line expanded because if I have 2 lines of comments, it adds a + and I don't want it.  I want to keep my comment visible for all lines.

    This is only for comments not the code

    geardoom3@live.com

  10. Doug says:

    Please add an Option to *not outline* comment blocks. This is super annoying. I just want to be able to right-click and Collapse all and see the comments above methods and the method {…}.  But because it treats multi-line comments as an outline block it collapses like 1/2 my method comments.

  11. Paul says:

    +1 for Doug's comment, I've wanted an option to 'not outline comments' since I first discovered the feature..

  12. Peter says:

    Please add an Outline By Filter so that auto outlining can outline every line of code that matches a text filter.

  13. Alex says:

    use this to have your comments not outlined :

       ''' <summary>

       ''' What is this Sub for….

       ''' </summary>

       ''' <param name="parameter_1">Description of parameter_1</param>

       ''' <param name="parameter_2">Description of parameter_2</param>

       ''' <param name="parameter_n">Description of parameter_n</param>

  14. Alex says:

    … in C# use

           /// <summary>

           ///

           /// </summary>

           /// <param name="args"></param>

  15. Eder says:

    What is the shortcut to toggle outline on cshtml views files on mvc project ?

  16. Doug says:

    Alex, we use C++ and our coding standards are to simply use multiple lines of "//" for pre-method comments:

    // XXXX…

    // YYYY…

    void Foo() { … }

    VS2013 Update 4 *still* collapses these lines if you have more than 1 comment line and it is extremely irritating.