How do I jump to the start or end of the current fold in Vim?
Answer
[z and ]z
Explanation
The [z and ]z commands jump the cursor to the start and end of the innermost open fold containing the cursor. They are motions, so they compose with operators and let you act on a fold's boundaries without manually counting lines.
How it works
[z— move cursor to the first line of the current fold]z— move cursor to the last line of the current fold- Both operate on the innermost fold containing the cursor position
- Since they are motions, they work with
d,y,c,=, and others
Example
Given an open fold for a Python function:
def process_data(items): ← [z lands here
for item in items:
transform(item) ← cursor starts here
validate(item)
return results ← ]z lands here
Pressing [z jumps to def process_data(items): and ]z jumps to return results.
Tips
- Use
zjandzkto jump to the next or previous fold (adjacent folds, not boundaries of the current one) - Combine with operators:
d]zdeletes from cursor to fold end;y[zyanks from fold start to cursor - These work with all fold methods:
indent,syntax,marker, andmanual - If the cursor is not inside any open fold, the cursor does not move