36

When I open a code file in a new code window, I press Ctrl+M,O to collapse everything there. As far as I know this can be done by default, without need to press anything every time. I think I did it once, but can't remember where was this option located.

Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 4
    Did you mean *expand*? I thougth the default *was* to collapse it. – Kirk Woll Jun 10 '11 at 21:30
  • If you started out with having unchecked outlining, you might find yourself in the position where you re-check it and want to test if it worked. In that situation, any files you already had open will retain its uncollapsed state. In fact, VS retains that state upon a restart, so you might think your change didn't work. However, if you open up other files you hadn't had open, it will default to collapsed. – Kirk Woll Jan 23 '14 at 03:50

4 Answers4

29

This is possible. Go to the Tools menu, then select options.

Text Editor
 \ C#
   \ Advanced

The option is called "Enter outlining mode when files open." When outlining mode is enabled, your regions are collapsed by default.

agent-j
  • 27,335
  • 5
  • 52
  • 79
  • 1
    @Peter Ivanov: Right, once you've opened a file, the outlining state gets saved (in the .suo I believe). The setting only affects files without a view state cached. – Tim Sparkles Feb 10 '16 at 19:57
6

Have you tried Tools\Options\Text Editor\C#\Advanced and check the "Enter outlining mode" when files open?

Simon Wilson
  • 9,929
  • 3
  • 27
  • 24
3

As a last resort if you can't get it to work with settings, you can also write a macro to do this. Check out this link for an example on this.

Here is the main information from the link:

You can open the Macro IDE by going to Tools->Macros->Macros IDE. There should be a module called EnvironmentEvents in project MyMacros. This code should be added to the EnvironmentEvents Module:

Private opened As Boolean

    Private Sub WindowEvents_WindowActivated(ByVal GotFocus As EnvDTE.Window, ByVal LostFocus As EnvDTE.Window) Handles WindowEvents.WindowActivated
        If GotFocus.Document Is Nothing Then
            Return
        End If
        If GotFocus.Document.FullName.EndsWith(".cs") And opened = True Then
            DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
        End If
        opened = False
    End Sub

    Private Sub DocumentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
        opened = True
End Sub
nickmoriarty
  • 1,253
  • 19
  • 21
  • 3
    Just an addendum to this answer, Macros were removed in Visual Studio 2012. – Nick Albrecht Apr 15 '13 at 15:55
  • So that's why I couldn't find it on my VS2015. What now? – dialex Feb 05 '16 at 16:05
  • You should put the key info from the link directly in the post. I know this is an older answer, so it's technically ok, but this is low quality as far as how answers are expected now. So a update would be nice. – Broots Waymb Dec 30 '16 at 17:31
  • 1
    @DangerZone I definitely should have added the relevant information from the link to the answer. I have updated the answer with that information now. Thanks. – nickmoriarty Jan 05 '17 at 00:13
0

For the record, I found unchecking the 'Enter Outlining Mode' option would disable all outlining, which was undesirable.

I did find this extension though: https://visualstudiogallery.msdn.microsoft.com/0ca60d35-1e02-43b7-bf59-ac7deb9afbca , the "I Hate #Regions" extension. Available for VS2010-2015, and so far seems to work as advertised.

CoderTao
  • 3,831
  • 1
  • 23
  • 18