Skip to content

Vim - Folding

Code folding is a feature that allows you to selectively hide (fold) and display (unfold) regions of text or code. This is particularly useful when working with large files, as it helps you focus on relevant sections by collapsing blocks like functions, classes, or indented code blocks.

This chapter discusses:

  • Enabling and configuring folding.
  • Common fold methods.
  • Commands for opening and closing folds.

To enable folding, you first need to set the foldenable option:

:set foldenable

Then, you must choose a foldmethod. This tells Vim how to determine which lines should be part of a fold. Some common methods are:

  • indent: Groups lines with the same indent level. Good for most programming languages.
  • :set foldmethod=indent
  • syntax: Folds based on syntax highlighting definitions. Many language syntax files include fold information (e.g., for functions, classes).
  • :set foldmethod=syntax
  • marker: Folds based on specific marker characters in the text (e.g., {{{ and }}}).
  • :set foldmethod=marker
  • manual: You create folds manually using commands like zf.
  • :set foldmethod=manual
  • expr: Defines folds using a custom expression (foldexpr). This is more advanced.

To disable folding, use:

:set nofoldenable

You can place these set commands in your vimrc file to apply them automatically.

Once folding is enabled and a method is set, you can use these Normal mode commands (usually starting with z) to manage folds. Ensure your cursor is on a line within or at the start of a potential fold region.

CommandDescription
zcClose one level of fold under the cursor.
zCClose all folds under the cursor recursively.
zoOpen one level of fold under the cursor.
zOOpen all folds under the cursor recursively.
zaToggle (open/close) the fold under the cursor.
zAToggle (open/close) the fold under the cursor recursively.

When a fold is closed, it typically appears as a single line with a summary of the folded content, e.g., +-- 42 lines: function my_function() ------. The exact appearance depends on your settings.

CommandDescription
zMClose all folds in the buffer (Fold More). Sets foldlevel to 0.
zROpen all folds in the buffer (Reduce folding). Sets foldlevel to a high value.
CommandDescription
zmIncrease foldlevel by one (close more folds).
zrDecrease foldlevel by one (open more folds).
CommandDescription
[zMove to the start of the current open fold. If not in an open fold, move to the start of the fold before it.
]zMove to the end of the current open fold. If not in an open fold, move to the end of the fold after it.
zjMove down to the start of the next fold (works like j but jumps over closed folds).
zkMove up to the end of the previous fold (works like k but jumps over closed folds).

Practical Application: In a long source code file, you can fold all functions except the one you’re currently working on. This reduces visual clutter and helps you maintain context, making it easier to understand the overall structure and focus on specific parts of the code.