NeuroAgent

Visual Studio Collapse/Expand Code: Complete Guide

Master Visual Studio code navigation with Ctrl+M+O and Ctrl+M+L shortcuts. Learn multiple methods to collapse/expand code sections efficiently across all VS versions.

Question

What is the command to collapse or expand all code sections in Visual Studio?

NeuroAgent

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

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:

  1. Place your cursor anywhere in the code editor
  2. Press Ctrl + M + O to collapse all code sections
  3. Press Ctrl + M + L to 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:

  1. Look for the Outlining toolbar (usually appears when you have code that can be collapsed)
  2. Click the Collapse to Definitions button (looks like a horizontal bracket pointing right)
  3. 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:

  1. Right-click anywhere in the code editor
  2. Navigate to Outlining in the context menu
  3. 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:

  1. Go to Tools > Options
  2. Navigate to Environment > Keyboard
  3. In the “Show commands containing” box, type one of:
    • Edit.CollapsetoDefinitions for collapse all
    • Edit.ExpandAll for expand all
  4. Select the command and click in the “Press shortcut keys” box
  5. Enter your preferred shortcut combination
  6. Click Assign and then OK

Common alternative shortcuts developers use:

  • Ctrl + - (minus) for collapse
  • Ctrl + + (plus) for expand
  • Ctrl + Shift + - for collapse
  • Ctrl + Shift + + for expand

Using the Mouse and Menus

Through the Edit Menu

  1. Click on Edit in the main menu
  2. Navigate to Outlining
  3. Select either:
    • Collapse to Definitions
    • Expand All

Through the Code Editor Context Menu

  1. Right-click anywhere in the code editor
  2. Select Outlining
  3. 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.

Sources

  1. Microsoft Visual Studio Documentation - Code Editor Features
  2. Visual Studio Shortcuts Guide - Code Outlining
  3. Stack Overflow - Visual Studio Collapse All Code
  4. Visual Studio Magazine - Code Navigation Tips
  5. Microsoft Learn - Customizing Keyboard Shortcuts