150

There's a number of keyboard shortcuts and menu commands to automatically expand or collapse all foldables in the current document. Ctrl+M, Ctrl+L toggles all foldables recursively, from the top namespace down to the inner methods and comments. Basically everything that has a [+] icon in the left margin. Ctrl+M, Ctrl+O does it bit less, but it still touches methods and comments inside the class.

I'm looking for a function that specifically only regards #region foldables and nothing else. Not namespaces, classes, comments, or methods. I really only want to fold those areas that are explicitly marked as such with the #region keyword.

Is there any hidden shortcut or an extension for that?

I'd prefer a keyboard solution, for Visual Studio 2015 (RC for now).

In case somebody asks why: I want to get an overview of the file, but immediately see all the details when I decide to look inside one region.


Update:

Solution in 2020

As this question has received many answers, some of them useless, others outdated, the working solution for Visual Studio 2019 can be found in this underappreciated answer here: https://stackoverflow.com/a/58999093/143684 Link to VS 2022 version in the comments.

ygoe
  • 18,655
  • 23
  • 113
  • 210
  • I don't think so (open to correction though). Have you tried class view - it will show all the class members (not the drop down at the top of the text editor) and you can double click to jump to the selected member. – amcdermott Jun 03 '15 at 12:55
  • 1
    I think there is no such thing, Ctrl+M,Ctrl+O is the closest – voytek Jun 03 '15 at 13:09
  • The Class View window contains the items all in one flat list, not "regioned" at all. That's not what I am using regions for. I need regions to quickly find members by topic, especially in long classes with a small number of similar methods each for a different model type. Regions group them by model type here. – ygoe Jun 03 '15 at 13:12

12 Answers12

269

in Visual Studio 2017 I have to activate 'Collapse #regions when collapsing to definitions' in

Tools -> Options -> Text Editor -> C# -> Advanced

explicitly to collapse all when pressing Ctrl+M+O

Broper
  • 2,000
  • 1
  • 14
  • 15
Myz
  • 2,791
  • 2
  • 8
  • 4
  • 9
    Unfortunately this also collapses everything else (almost), not just regions. So this is not what I'm looking for. The Quick Launch Tasks extension still exists for VS 2017 and can be installed separately now, see me other comment. – ygoe Apr 18 '17 at 20:33
  • 25
    This might not be what the OP asked for, but it's what I was looking for. – Homer Jun 01 '17 at 14:04
  • 11
    The option in the current build is "Collapse #regions when collapsing to definitions" and it's located in Tools > Options > Text Editor > C# > Advanced. This is what I was looking for as well. This makes Ctrl+M+O work like it did in VS 2015 – Matt Becker Jul 11 '17 at 18:05
  • Thank you! Just couldn't recall how to do this after I changed my laptop, and the exported settings seemed to misbehave. This answer saved me a lot of time. – Pradeep Puranik Jul 27 '23 at 08:55
81

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.

These options are also in the context menu under Outlining.

Right click in editor -> Outlining to find all options. (After disabling outlining, use same steps to enable outlinging.)

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Brendon Vdm
  • 914
  • 5
  • 5
46

To collapse regions : Tools > Options > Text Editors > C# > Advanced > Check "Collapse #regions when collapsing to definitions"

Collapse #regions image

Taazar
  • 1,545
  • 18
  • 27
Osama Nour
  • 471
  • 4
  • 6
36

Update for VS2022: https://github.com/mdmower/CollapseRegionExtension/releases/tag/v1.5 (thanks @ygoe)

Alternate marketplace download (same extension different author): https://marketplace.visualstudio.com/items?itemName=CMPhys.ToggleRegions2022 (thanks @Memphis)


I know this is an old question, but here's an update for visual studio 2019:

The Quick Tasks solution from @ygoe's answer does not work for Visual Studio 2019, because that version changed the way the Quick Task bar searches. Plus it was never the preferred solution to the original question, since you needed to type a quick action instead of having a keyboard shortcut.

I found a different solution on the marketplace which does exactly the collapsing and expanding of all regions in the currently opened file with keyboard shortcuts: Collapse Region

From the extension page:

Default key bindings are (Ctrl+R, Ctrl+Num+) and (Ctrl+R, Ctrl+Num-). Can be changed in Tools->Options->Environment->Keyboard.

Chronicle
  • 1,565
  • 3
  • 22
  • 28
  • Thank you for this great info! Since I'm not using quick tasks for much else, I might just switch to that extension. Tested and works (in VS 2017 and 2019). – ygoe Nov 23 '19 at 15:14
  • 1
    Currently the update for VS 2022 is available here: https://github.com/mdmower/CollapseRegionExtension – ygoe Nov 29 '22 at 17:23
  • 1
    Yep, I've been using Collapse Region for a while and started missing it now that I switched to VS2022. The original may be abandoned, however Matt Mower brought it back to life (thanks Matt!) and he also released it on the Marketplace: https://marketplace.visualstudio.com/publishers/CMPhys – Memphis Mar 03 '23 at 12:47
30

The Visual Studio extension Productivity Power Tools 2015 from Microsoft has a feature called Quick Launch Tasks that adds new commands to the Quick Launch menu. One of them is CollapseRegions and it does exactly that.

The opposite command is ExpandRegions and it expands all regions for quick browsing of the entire file. These commands can be used pretty quickly by pressing the CtrlQ hotkey and typing Coll resp. Exp, then pressing Enter (supposed you don't have other commands with the same prefix).

ygoe
  • 18,655
  • 23
  • 113
  • 210
  • 7
    Here is the corresponding extension for Visual Studio 2017: [Quick Launch Tasks](https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.QuickLaunchTasks) – ygoe Apr 18 '17 at 20:28
  • No longer valid with Visual Studio 2019 because they changed the way Quick Tasks search works. – Chronicle Nov 22 '19 at 17:30
25

tick the Checkmark in Settings -> TextEditor -> C# -> Advanced -> (section Outlining) Collapse #regions when collapsing definitions.

then right click in the editor > outlining > collapse to definitions

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Ninja Dev
  • 816
  • 8
  • 10
13

I dont know if this is new, but there is a setting for the c# text edior:

Settings -> TextEditor -> C# -> Advanced -> Collapse #regions when collapsing definitions.

When its set CtrlM CtrlO will collapse the #regions.

Roland
  • 133
  • 1
  • 6
8

Looks like the closest thing is Ctrl+M, Ctrl+S

Which will collapse the current region you are in, while Ctrl+M, Ctrl+E will expand the current region you are in.

List of default keyboard shortcuts:

https://msdn.microsoft.com/en-us/library/da5kh0wa(v=vs.140).aspx

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
AzNjoE
  • 733
  • 3
  • 9
  • 18
3

I recommend using the free Visual Studio extension "Menees VS Tools Extension for Visual Studio." Just search for it in Tools --> Extensions and Updates in Visual Studio. Once installed you just have to hit Ctrl+M+K and just the regions will be collapsed. Here is their website https://marketplace.visualstudio.com/items?itemName=BillMenees.MeneesVSTools2013

To edit the settings look under Tools --> Options --> Menees VS Tools --> General.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
TheJonz
  • 394
  • 2
  • 11
2

For Visual Studio 2022, here is the tool that works well:

https://marketplace.visualstudio.com/items?itemName=EngineDesigns.CollapseAllRegions

CTRL-M CTRL-R

to collapse all #region groups.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30350171) – Draken Nov 16 '21 at 09:43
  • Agreed with you. This question posed by OP - which I provided the answer, as I have the same issue as OP and others. I also noticed that many answers do not meet our needs. This Engine Designs author who wrote this tool has solved the problem we are seeking - for now. :) – Robert Rademacher Nov 16 '21 at 10:58
0

Not sure about previous Visual Studio versions, but in VS 2022 we have new shortcut

CTRL + M + M

It will collapse any part of code like namespace, class, region etc.,

Steps:

  1. Go to #region piece of code.
  2. Select #region or click on #region anywhere in that word.
  3. From Keyboard click CTRL + M + M

If you want for entire namespace to collapse, you can do same steps by selecting or clicking namespace.

PavanKumar GVVS
  • 859
  • 14
  • 45
  • This has already been answered over 3 years ago and downvoted (see below). How does your answer differ from that? – ygoe Nov 29 '22 at 17:22
-3

Select All via Ctrl A and then Ctrl M M

i.e. Press M twice while holding down Ctrl

  • Sorry, no. This collapses **everything** including namespaces, classes, methods and comments. This was explicitly not asked for. – ygoe Feb 15 '19 at 19:15
  • In visual studio For javascript regions collapse Ctrl+M+O is not working but selecting all items then Ctrl+M+M is working, thanks – Serhat Oz Jan 09 '21 at 12:17