Folding

Program source code and other structured text files can be thought of as containing a hierarchy of sections, which themselves might contain sub-sections. The folding feature lets you selectively hide and show these sections, replacing hidden ones with a single line that serves as an “overview” of that section.

Folding is disabled by default. To enable it, you must choose one of the available folding modes. “Indent” mode creates folds based on a line's leading whitespace; the more leading whitespace a block of text has, the further down it is in the hierarchy. For example:

This is a section
  This is a sub-section
  This is another sub-section
    This is a sub-sub-section
Another top-level section

Explicit” mode folds away blocks of text surrounded with “{{{” and “}}}”. For example:

{{{ The first line of a fold.
When this fold is collapsed, only the above line will be visible.

{{{ A sub-section.
With text inside it.
}}}

{{{ Another sub-section.
}}}

}}}

Both modes have distinct advantages and disadvantages; indent folding requires no changes to be made to a buffer's text and does a decent job with most program source. Explicit folding requires “fold markers” to be inserted into the text, but is more flexible in exactly what to fold away.

Some plugins might add additional folding modes; see Chapter 8, Installing and Using Plugins for information about plugins.

Folding can be enabled in one of several ways:

Warning

When using indent folding, portions of the buffer may become inaccessible if you change the leading indent of the first line of a collapsed fold. If you experience this, you can use the Expand All Folds command to make the text visible again.

Collapsing and Expanding Folds

The first line of each fold has a triangle drawn next to it in the gutter (see Section , “The Text Area and Gutter” for more information about the gutter). The triangle points toward the line when the fold is collapsed, and downward when the fold is expanded. Clicking the triangle collapses and expands the fold. To expand all sub-folds as well, hold down the Shift while clicking.

The first line of a collapsed fold is drawn with a different background color, and the number of lines in the fold is shown to the right of the line's text.

Folds can also be collapsed and expanded using menu item commands and keyboard shortcuts.

Folding>Collapse Fold (keyboard shortcut: Alt-Backspace) collapses the fold containing the caret position.

Folding>Expand Fold One Level (keyboard shortcut: Alt-Enter) expands the fold containing the caret position. Nested folds will remain collapsed, and the caret is positioned on the first nested fold (if any).

Folding>Expand Fold Fully (keyboard shortcut: Alt-Shift-Enter) expands the fold containing the caret position, also expanding any nested folds.

Folding>Collapse All Folds (keyboard shortcut: Control-E C) collapses all folds in the buffer.

Folding>Expand All Folds (keyboard shortcut: Control-E X) expands all folds in the buffer.

Navigating Around With Folds

Folding>Go to Parent Fold (keyboard shortcut: Control-e u) moves the caret to the fold containing the one at the caret position.

Folding>Go to Previous Fold (keyboard shortcut: Alt-Up) moves the caret to the fold immediately before the caret position.

Folding>Go to Next Fold (keyboard shortcut: Alt-Down) moves the caret to the fold immediately after the caret position.

Miscellaneous Folding Commands

Folding>Add Explicit Fold (keyboard shortcut: Control-E A) is a convenience command that surrounds the selection with “{{{” and “}}}”. If the current buffer's edit mode defines comment strings (see Section , “Commenting Out Code”) the explicit fold markers will automatically be commented out as well.

Folding>Select Fold (keyboard shortcut: Control-E S) selects all lines within the fold containing the caret position. Control-clicking a fold expansion triangle in the gutter has the same effect.

Folding>Expand Folds With Level (keyboard shortcut: Control-E Enter key) reads the next character entered at the keyboard, and expands folds in the buffer with a fold level less than that specified, while collapsing all others.

Sometimes it is desirable to have files open with folds initially collapsed. This can be configured as follows:

  • On a global or mode-specific basis in the Editing pane of the Utilities>Global Options dialog box.

  • In the current buffer for future editing sessions by placing the following in the first or last 10 lines of a buffer, where level is the desired fold level:

    :collapseFolds=level:

Narrowing

The narrowing feature temporarily “narrows” the display of a buffer to a specified region. Text outside the region is not shown, but is still present in the buffer.

Folding>Narrow Buffer to Fold (keyboard shortcut: Control-E N N) hides all lines the buffer except those in the fold containing the caret.

Folding>Narrow Buffer to Selection (keyboard shortcut: Control-E N S) hides all lines the buffer except those in the selection.

Folding>Expand All Folds (keyboard shortcut: Control-E X) shows lines that were hidden as a result of narrowing.