IntelliJ IDEA 2016.2 Help

Code Folding

On this page:

Basics

You can collapse (fold) code fragments reducing them to a single visible line. In this way, you can hide the details that, at the moment, seem unimportant. If and when necessary, the folded code fragments can be expanded (unfolded).

Folded code fragments, normally, are shown as shaded ellipses (foldedFragment).

ij_foldingGeneral

Code folding means

You can collapse and expand code fragments by using:

  • Code folding toggles (foldingMinusStart, foldingMinusEnd or foldingPlus). These toggles are shown in the editor to the left of the corresponding folding regions. If a region is unfolded, foldingMinusStart indicates the beginning of the region while foldingMinusEnd is located at its end. For folded regions, the toggle is shown as foldingPlus.

    If you hold the Alt key and click foldingMinusStart, foldingMinusEnd or foldingPlus, the region is collapsed or expanded recursively, i.e. along with all its subordinate regions.

  • Commands of the Folding menu and associated keyboard shortcuts. The Folding menu can be accessed from the main menu bar (Code | Folding), or as a context menu in the editor. The shortcuts are shown right in the menu. See Commands of the Folding menu and associated shortcuts.
  • Folded fragments themselves: click foldedFragment to expand the corresponding fragment. See also, Viewing folded code fragments.

Folding predefined and custom regions

You can fold and unfold:

  • Code blocks, i.e. code fragments surrounded by a matched pair of curly braces {}.

    To collapse a code block, place the cursor within that block and then select Code | Folding | Fold Code Block or press Ctrl+Shift+Period.

    As a result, a custom folding region is formed, and the folding toggles for it appear. After that, the region can be collapsed and expanded as any other folding region.

    To remove a custom folding region, use the Fold Selection / Remove Region command for that region (Ctrl+Period).

  • Predefined regions that correspond to such elements as import declarations, method bodies, anonymous and inner classes, documentation comments, etc. The predefined regions, roughly, correspond to the ones listed under Collapse by default on the Editor | General | Code Folding page in the Settings/Preferences dialog.

    For the predefined regions, the folding toggles are available right away, without the need to perform any additional actions.

  • Any selected code fragment. A custom folding region for a selection is created and removed by means of the Fold Selection/ Remove Region command (Ctrl+Period).
  • Regions surrounded by corresponding commented folding markers (e.g. //<editor-fold desc="Description">...//</editor-fold>). See Using code folding comments.

Commands of the Folding menu and associated shortcuts

The Folding menu can be accessed from the main menu bar (Code | Folding), or as a context menu in the editor.

CommandShortcutDescription
Expand Ctrl+NumPad Plus Expand the current collapsed fragment
Collapse Ctrl+NumPad - Collapse the current folding region
Expand Recursively Ctrl+Alt+NumPad Plus Expand the current folded fragment and all the subordinate collapsed folding regions within that fragment
Collapse Recursively Ctrl+Alt+NumPad - Collapse the current folding region and all the subordinate folding regions within it
Expand All Ctrl+Shift+NumPad Plus Expand all collapsed fragments within the selection, or, if nothing is selected, expand all the collapsed fragments in the current file
Collapse All Ctrl+Shift+NumPad - Collapse all folding regions within the selection, or, if nothing is selected, collapse all the folding regions in the current file
Expand to level | 1, 2, 3, 4 or 5 Ctrl+NumPad *, 1
Ctrl+NumPad *, 2
Ctrl+NumPad *, 3
Ctrl+NumPad *, 4
Ctrl+NumPad *, 5
Expand the current fragment and all the nested fragments up to the specified level
Expand all to level | 1, 2, 3, 4 or 5 Ctrl+Shift+NumPad *, 1
Ctrl+Shift+NumPad *, 2
Ctrl+Shift+NumPad *, 3
Ctrl+Shift+NumPad *, 4
Ctrl+Shift+NumPad *, 5
Expand all the collapsed fragments in the file up to the specified nesting level
Expand doc comments Expand all documentation comments in the current file
Collapse doc comments Collapse all documentation comments in the current file
Fold Selection / Remove region Ctrl+Period Collapse the selected fragment and create a custom folding region for it to make it "foldable" / Expand the current fragment and remove the corresponding custom folding region to make the fragment "unfoldable"
Fold Code Block Ctrl+Shift+Period Collapse the code fragment between the matched pair of curly braces {} and create a custom folding region for that fragment to make it "foldable"

Specifying code folding preferences

You can specify:

  • Whether the code folding toggles should be shown.
  • Which folding regions should be collapsed by default.

The corresponding settings are in the Settings dialog (Ctrl+Alt+S) on the Editor | General | Code Folding page.

For more information, see Code Folding page.

Viewing folded code fragments

To see the contents of a folded fragment, point to the ellipsis foldedFragment that indicates that fragment.

ij_foldingPopup

Viewing the beginning of a folding region

To see the beginning of a folding region - if it's not currently visible - point to the folding toggle at the end of that region.

ij_view_invisible_code

Using code folding comments

Supported folding comments

You can create custom folding regions by surrounding code fragments with the commented lines. So doing, the comments can be either NetBeans style, or Visual Studio style.

NetBeans style

//<editor-fold desc="Description"> ... //</editor-fold>

Visual Studio style

//region Description ... //endregion

Once you have chosen the style for a file, don't use the other style in that file.

Surrounding a fragment with folding comments

  1. Select the code fragment of interest.
  2. Select Code | Surround With or press Ctrl+Alt+T.
  3. Select the folding comments to be used.
    ij_customFolding1
  4. Specify the fragment description.
    ij_customFolding2

    Now if you collapse the fragment, the description you have specified is shown in place of the code.

    ij_customFolding3

Navigating to folding regions

You can navigate to custom folding regions that were formed by surrounding code fragments with the corresponding commented folding markers:

  1. Select Navigate | Custom Folding or press Ctrl+Alt+Period.
  2. Select the target folding region. (The regions in the list are identified by their descriptions.)
    ij_navigateToCustomRegion

See Also

Getting Started:

Last modified: 8 August 2016