What is the command to collapse or expand all code sections in Visual Studio?
The primary keyboard shortcut to collapse all code sections in Visual Studio is Ctrl + M + O, and to expand all code sections, it’s Ctrl + M + L. These shortcuts work in most Visual Studio versions and can be customized through the Tools > Options > Environment > Keyboard menu if needed.
Contents
- Primary Keyboard Shortcuts
- Alternative Methods for Collapsing/Expanding Code
- Version-Specific Variations
- Customizing Shortcuts in Visual Studio
- Using the Mouse and Menus
- Third-Party Extensions for Enhanced Functionality
Primary Keyboard Shortcuts
The most efficient way to collapse or expand all code sections in Visual Studio is through keyboard shortcuts:
- Collapse All:
Ctrl + M + O - Expand All:
Ctrl + M + L
These shortcuts work by first pressing Ctrl + M, then releasing both keys and pressing either O (for “Outlining”) or L (for “Line”).
Important: These shortcuts work across multiple programming languages and file types that support code outlining, including C#, VB.NET, C++, JavaScript, TypeScript, and Python files.
How to use them:
- Place your cursor anywhere in the code editor
- Press
Ctrl + M + Oto collapse all code sections - Press
Ctrl + M + Lto expand all code sections back to their original state
These shortcuts are particularly useful when working with large files where you need to quickly navigate the overall structure of your code.
Alternative Methods for Collapsing/Expanding Code
Using the Toolbar Buttons
Visual Studio provides toolbar buttons for outlining operations:
- Look for the Outlining toolbar (usually appears when you have code that can be collapsed)
- Click the Collapse to Definitions button (looks like a horizontal bracket pointing right)
- Click the Expand All button (looks like a horizontal bracket pointing left)
Right-Click Menu Options
You can also access these features through the context menu:
- Right-click anywhere in the code editor
- Navigate to Outlining in the context menu
- Select Collapse to Definitions or Expand All
Individual Region Control
For more granular control, you can:
- Collapse individual regions by clicking the minus (-) icon next to the region name
- Expand collapsed sections by clicking the plus (+) icon
- Double-click the region line to toggle its state
Version-Specific Variations
Visual Studio 2019 and Later
In Visual Studio 2019 and newer versions, these shortcuts are consistent across editions:
- Professional:
Ctrl + M + O/Ctrl + M + L - Enterprise:
Ctrl + M + O/Ctrl + M + L - Community:
Ctrl + M + O/Ctrl + M + L
Visual Studio 2017
Visual Studio 2017 uses the same shortcuts but may have slight variations in the menu structure:
- Collapse All:
Ctrl + M + O - Expand All:
Ctrl + M + L
Visual Studio 2015 and Earlier
Older versions of Visual Studio used the same shortcuts but with different menu labeling:
- Collapse All:
Ctrl + M + O - Expand All:
Ctrl + M + L
| Version | Collapse Shortcut | Expand Shortcut | Menu Location |
|---|---|---|---|
| VS 2022 | Ctrl + M + O |
Ctrl + M + L |
Edit > Outlining |
| VS 2019 | Ctrl + M + O |
Ctrl + M + L |
Edit > Outlining |
| VS 2017 | Ctrl + M + O |
Ctrl + M + L |
Edit > Outlining |
| VS 2015 | Ctrl + M + O |
Ctrl + M + L |
Edit > Outlining |
| VS 2013 | Ctrl + M + O |
Ctrl + M + L |
Edit > Outlining |
Customizing Shortcuts in Visual Studio
If you prefer different keyboard shortcuts or need to assign them to other commands, you can customize them:
- Go to Tools > Options
- Navigate to Environment > Keyboard
- In the “Show commands containing” box, type one of:
Edit.CollapsetoDefinitionsfor collapse allEdit.ExpandAllfor expand all
- Select the command and click in the “Press shortcut keys” box
- Enter your preferred shortcut combination
- Click Assign and then OK
Common alternative shortcuts developers use:
Ctrl + -(minus) for collapseCtrl + +(plus) for expandCtrl + Shift + -for collapseCtrl + Shift + +for expand
Using the Mouse and Menus
Through the Edit Menu
- Click on Edit in the main menu
- Navigate to Outlining
- Select either:
- Collapse to Definitions
- Expand All
Through the Code Editor Context Menu
- Right-click anywhere in the code editor
- Select Outlining
- Choose your desired action:
- Toggle Outlining
- Toggle All Outlining
- Collapse to Definitions
- Expand All
Using the Outlining Bar
Many code editors display an outlining bar on the left side when code can be collapsed:
- Click the small arrows to expand/collapse individual sections
- Double-click the region line to toggle its state
- Use the outlining bar’s collapse/expand buttons for all sections
Third-Party Extensions for Enhanced Functionality
For developers who need more advanced outlining capabilities, several extensions are available:
Productivity Power Tools
This Microsoft extension provides enhanced outlining features:
- Better visual indicators for collapsible regions
- Improved performance with large files
- Customizable outlining behaviors
- Additional keyboard shortcuts
Visual Studio IntelliCode
Provides AI-powered code completion and can suggest optimal outlining patterns:
- Intelligent region suggestions
- Context-aware outlining recommendations
- Performance optimizations for large codebases
CodeMaid
A free extension that includes advanced outlining features:
- Mass cleanup and organization tools
- Enhanced outlining controls
- Customizable outlining rules
- Bulk operations on code sections
Conclusion
Mastering the collapse/expand shortcuts in Visual Studio significantly improves code navigation efficiency. The primary shortcuts Ctrl + M + O and Ctrl + M + L work across all recent versions and provide quick access to code structure management. For enhanced functionality, consider using toolbar buttons, context menus, or third-party extensions like Productivity Power Tools. Remember that shortcuts can be customized through Tools > Options > Environment > Keyboard if you prefer different key combinations. These tools help maintain clean, organized code views while preserving quick access to implementation details when needed.