ygoe
ygoe

Reputation: 20414

Collapse all #regions only(!) in C# (Visual Studio)

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.

Upvotes: 160

Views: 136312

Answers (13)

Andy Brown
Andy Brown

Reputation: 5522

From the Edit menu in Visual Studio 2022 you can see this:

enter image description here

So press CTRL + R followed by CTRL + the - minus key on your numeric keypad to do this. I've been looking for this for years - not sure if it's just been added?

Upvotes: 0

Chronicle
Chronicle

Reputation: 1645

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.

Upvotes: 42

PavanKumar GVVS
PavanKumar GVVS

Reputation: 1045

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.

Upvotes: 1

Ninja Dev
Ninja Dev

Reputation: 836

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

Upvotes: 25

Robert Rademacher
Robert Rademacher

Reputation: 66

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.

Upvotes: 2

Osama Nour
Osama Nour

Reputation: 551

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

Collapse #regions image

Upvotes: 54

Myz
Myz

Reputation: 2941

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

Upvotes: 284

Roland
Roland

Reputation: 143

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.

Upvotes: 14

user1979649
user1979649

Reputation: 17

Select All via Ctrl A and then Ctrl M M

i.e. Press M twice while holding down Ctrl

Upvotes: -3

TheJonz
TheJonz

Reputation: 414

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.

Upvotes: 3

AzNjoE
AzNjoE

Reputation: 733

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

Upvotes: 8

Brendon Vdm
Brendon Vdm

Reputation: 974

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.)

Upvotes: 87

ygoe
ygoe

Reputation: 20414

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).

Upvotes: 30

Related Questions