How to: Outline and Hide Code

By default, all text is displayed in the Code Editor, but you can choose to hide some code from view. The Code Editor lets you select a region of code and make it collapsible, so that it appears under a plus sign (+).

Code After Collapse

Click the plus sign (+) next to the symbol to expand or hide the region. Outlined code is not deleted, it is hidden from view.

Code before collapse

The Undo and Redo commands on the Edit menu affect these actions. In other words, if you create a collapsible region, you can choose Undo to reverse that action. Also, Copy, Cut, Paste, and drag-and-drop operations retain outlining information, but not the state of the collapsible region. For example, when you copy a region that is collapsed, the Paste operation will paste the copied text as an expanded region.

After outlining code, you can expand the sections you want to work on, collapse them when you are done, and then move to other sections. When you are through outlining, you can use the Stop Outlining command to remove the outline information without disturbing your underlying code.

Warning

When you invalidate the region, the outlined region is lost. For example, deleting or Find and Replace operations may erase the end of the region.

The following commands are available from the Outlining submenu on the Edit menu and on the shortcut menu of the Code Editor:

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

Command

Definition

Hide Selection

Collapses the currently selected text. Text must be selected to use this command. Shortcut keys are CTRL+M and then CTRL+H.

NoteNote
Not available in Visual Basic.

Toggle Outlining Expansion

Reverses the current hidden or expanded state of the innermost outlining section in which the cursor lies when you are in a nested collapsed section. Shortcut keys are CTRL+M and then CTRL+M.

Toggle All Outlining

Sets all procedures to the same hidden or expanded state. If some regions are expanded and some hidden, then the hidden regions are expanded. Shortcut keys are CTRL+M and then CTRL+L.

Stop Outlining

Removes all outlining information for the entire document. All regions are visible and the symbols removed, but the underlying code is undisturbed. Shortcut keys are CTRL+M and then CTRL+P.

Start Automatic Outlining

This command is only available when Automatic Outlining is turned off. Use this command to enable Automatic Outlining in the IDE.

Stop Hiding Current

Removes the outlining information for the currently selected user-defined region. Shortcut keys are CTRL+M and then CTRL+U.

Note   Not available in Visual Basic.

Collapse to Definitions

Collapses the members of all types so that all of the members that belong to a type are clearly seen. Identical to the Collapse Block command except that the environment automatically runs through all of the code in the document, creates regions in each of your procedures, and then hides them. Shortcut keys are CTRL+M and then CTRL+O.

Collapse Block: <logical boundary>

(Visual C++) The environment uses the programming language's service to automatically determine logical boundaries for creating a region in the function containing the insertion point, and then hides it. For example, if the insertion point lies inside a loop, a region is created for that statement and the region is hidden. Each time you execute this command, the next higher-level containing statement is collapsed until you end up at the top of a procedure or other top-level definition in your code.

Collapse All in: <logical structures>

(Visual C++) Performs the same function as Collapse Block, except that it recursively collapses all logical structures inside the function in a single step.

Creating Collapsible Sections of Code in Visual C++

To create collapsible sections of code

  1. Select the desired section of code or text.

    Note

    To enable the Hide Selection command, select a continuous stream of text.

  2. Right-click on the selection, and select Outlining from the shortcut menu.

  3. Choose Hide Selection from the Outlining submenu.

    The selection collapses under the top-most selected line and a plus sign (+) appears to the left of the line in the indicator margin.

To make code enclosed in braces collapsible

  1. Right-click in the Code Editor on a section of Visual C++ code enclosed in braces ("{}"), and select Outlining from the shortcut menu.

  2. Choose Collapse Block from the Outlining submenu.

    A minus sign (-) appears in the indicator margin next to the line with the opening brace ("{"). When you select this sign, all subsequent lines up to the matching closing brace ("}") collapse under the top-most line of this section, and a plus-sign (+) symbol appears.

    Note

    This option is enabled when you are programming in Visual C++.

To make logical structures enclosed in braces collapsible

  1. Right-click in the Code Editor on a section of Visual C++ code enclosed in braces ("{}") that includes logical structures that are also enclosed in braces ("{}"), and select Outlining from the shortcut menu.

  2. Choose Collapse All in: <logical structure> from the Outlining submenu.

    A minus sign (-) appears in the indicator margin beside the opening brace ("{") of each subsection of code that is also enclosed in braces. When you select this sign, all subsequent lines up to the matching closing brace ("}") collapse under the top-most line of the subsection, and a plus-sign (+) symbol appears.

    Note

    This option is available only when you are programming in Visual C++.

Collapsing and Expanding Code

To collapse and expand a section of code

  1. To display all lines in a collapsed section, click the plus sign (+) in the indicator margin.

  2. To conceal an expanded section, click the minus sign (-) in the indicator margin.

    You can also use the commands Toggle Outlining Expansion or Toggle All Outliningto hide all collapsible sections or make all of them visible.

To collapse and expand several collapsible sections of code

  1. Select contiguous sections of collapsible code in the Code Editor.

  2. Right-click on the selection, and select Outlining from the shortcut menu.

  3. Choose Toggle Outlining Expansion from the Outlining submenu.

    If any selected section is expanded, then all selected sections are collapsed. If all selected sections are collapsed, then they are all expanded.

To collapse or expand all collapsible sections of code

  1. Right-click anywhere in the Code Editor, and select Outlining from the shortcut menu.

  2. Choose Toggle All Outlining from the Outlining submenu.

    If any collapsible section is collapsed, then all sections are expanded.

    If all sections are expanded, then they are all collapsed.

To collapse all definitions

  1. Right-click anywhere in the Code Editor, and select Outlining from the shortcut menu.

  2. Choose Collapse to Definitions from the Outlining submenu.

    All collapsible sections within the current module are collapsed.

Automatic Outlining

Various development languages support an editing mode where automatic outlining is always on. As you type new definitions, the Code Editor automatically creates collapsible sections around the blocks of new code. Once you have selected the option that enables outlining mode in your development language, commands become available from the shortcut menu of the Code Editor that expand and collapse selected sections, all definitions, and every collapsible section, and toggles automatic outlining off and on.

To create collapsible sections of code automatically

  1. Select the option that enables outlining mode for the language in which you are programming.

    For further information on options that enable Automatic Outlining in specific programming languages, see VB Specific, Basic, Text Editor, Options Dialog Box, Formatting, C/C++, Text Editor, Options Dialog Box, and Formatting, C#, Text Editor, Options Dialog Box.

  2. Add a new procedure, function, enumeration, or other code block to your code.

    A minus sign (-) appears in the indicator margin next to the topmost line. When you select this sign, the added code collapses under the top-most line, and a plus-sign (+) symbol appears.

To turn off outlining for one collapsible section

  1. Right-click on a collapsible section in the Code Editor, and select Outlining from the shortcut menu.

  2. Choose Stop Hiding Current.

    The section is expanded, and the minus sign (-) to collapse it is no longer displayed in the indicator margin.

To turn off automatic outlining and expand all collapsed sections

  1. Right-click anywhere in the Code Editor, and select Outlining from the shortcut menu.

  2. Choose Stop Outlining.

    Any collapsed sections are expanded, and minus signs (-) to collapse are no longer displayed in the indicator margin.

To restore automatic outlining and collapse all expanded sections

  1. Right-click anywhere in the Code Editor, and select Outlining from the shortcut menu.

  2. Choose Start Automatic Outlining.

    Any collapsible sections are collapsed under their top-most lines and plus signs (+) to expand them are displayed in the indicator margin.

See Also

Tasks

How to: Collapse and Hide Sections of Code (Visual Basic)

Reference

#Region Directive

#If...Then...#Else Directives

Concepts

Editor Convenience Commands and Features

Customizing the Editor

Other Resources

Editing Text, Code, and Markup