vimtricks.wiki Concise Vim tricks, one at a time.

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():-----
  1. With the cursor on the first fold, press zj to jump to the validate_input fold.
  2. Press zj again to jump to generate_report.
  3. Press zk to jump back to validate_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

  • [z moves to the start of the current open fold.
  • ]z moves to the end of the current open fold.
  • zM closes all folds, and zR opens all folds.

Next

How do you yank a single word into a named register?