How do I jump between folds in Vim without opening them?
Answer
zj and zk
Explanation
How it works
When working with folded code in Vim, you often want to skip from one fold to another without unfolding anything. Vim provides two motions for this:
- zj moves the cursor downward to the start of the next fold. If the cursor is currently on a closed fold, it jumps to the next fold below it.
- zk moves the cursor upward to the end of the previous fold. If the cursor is on a closed fold, it jumps to the fold above it.
These commands treat folds as navigable landmarks in your file. They work regardless of the fold method you are using (manual, indent, syntax, marker, expr, or diff).
Unlike zo and zc which open and close folds, zj and zk are pure navigation commands. They leave the fold state untouched and simply move between fold boundaries.
Example
Suppose you have a Python file with several functions, and you are using indent-based folding (:set foldmethod=indent):
+-- 10 lines: def process_data():--------
+-- 8 lines: def validate_input():------
+-- 15 lines: def generate_report():-----
- With the cursor on the first fold, press
zjto jump to thevalidate_inputfold. - Press
zjagain to jump togenerate_report. - Press
zkto jump back tovalidate_input.
This lets you quickly survey the structure of a file. Combine with zo to open an interesting fold once you find it, or with zO to recursively open all nested folds at that position.
Related commands
[zmoves to the start of the current open fold.]zmoves to the end of the current open fold.zMcloses all folds, andzRopens all folds.