Vim - Folding
Vim - Code Folding
Section titled “Vim - Code 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.
Enabling and Configuring Folding
Section titled “Enabling and Configuring Folding”To enable folding, you first need to set the foldenable option:
:set foldenableThen, 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 likezf.-
:set foldmethod=manual
expr: Defines folds using a custom expression (foldexpr). This is more advanced.
To disable folding, use:
:set nofoldenableYou can place these set commands in your vimrc file to apply them automatically.
Fold Actions (Normal Mode Commands)
Section titled “Fold Actions (Normal Mode Commands)”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.
Opening and Closing Folds
Section titled “Opening and Closing Folds”| Command | Description |
|---|---|
zc | Close one level of fold under the cursor. |
zC | Close all folds under the cursor recursively. |
zo | Open one level of fold under the cursor. |
zO | Open all folds under the cursor recursively. |
za | Toggle (open/close) the fold under the cursor. |
zA | Toggle (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.
Opening and Closing All Folds
Section titled “Opening and Closing All Folds”| Command | Description |
|---|---|
zM | Close all folds in the buffer (Fold More). Sets foldlevel to 0. |
zR | Open all folds in the buffer (Reduce folding). Sets foldlevel to a high value. |
Adjusting Fold Levels
Section titled “Adjusting Fold Levels”| Command | Description |
|---|---|
zm | Increase foldlevel by one (close more folds). |
zr | Decrease foldlevel by one (open more folds). |
Navigating Folds
Section titled “Navigating Folds”| Command | Description |
|---|---|
[z | Move to the start of the current open fold. If not in an open fold, move to the start of the fold before it. |
]z | Move to the end of the current open fold. If not in an open fold, move to the end of the fold after it. |
zj | Move down to the start of the next fold (works like j but jumps over closed folds). |
zk | Move 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.