How do I scroll the screen to position the cursor line at the center, top, or bottom?
Answer
zz / zt / zb
Explanation
Vim's z scroll commands reposition the screen relative to the cursor without moving the cursor itself. This is essential for getting context around your current position — centering a function definition, or pushing it to the top to see more of its body.
How it works
zz— scroll to center the cursor line on screenzt— scroll to place the cursor line at the topzb— scroll to place the cursor line at the bottom- The cursor stays on the same line — only the viewport moves
Example
Before zz (cursor on line 50, shown near bottom):
...
line 48
line 49
line 50 ← cursor (near bottom of screen)
After zz:
line 40
...
line 50 ← cursor (centered on screen)
...
line 60
Tips
ztis perfect after jumping to a function definition — shows the full function body belowz.centers likezzbut also moves cursor to the first non-blank characterz<CR>is likeztbut also moves cursor to first non-blank- Combine with
scrolloff::set scrolloff=5keeps 5 lines of context above/below cursor always