How do I scroll the current line to the center or top of the screen and simultaneously move my cursor to the first non-blank character?
Answer
z. and z<CR> and z-
Explanation
Vim has two parallel sets of scroll commands: the well-known zz, zt, zb which reposition the view without moving the cursor, and the lesser-known z., z<CR>, z- which do the same repositioning and move the cursor to the first non-blank character of the line.
How it works
| Command | Scroll position | Cursor moves to |
|---|---|---|
zz |
Center line in window | stays in place |
z. |
Center line in window | first non-blank |
zt |
Line to top of window | stays in place |
z<CR> |
Line to top of window | first non-blank |
zb |
Line to bottom of window | stays in place |
z- |
Line to bottom of window | first non-blank |
The cursor-moving variants are useful when your cursor is mid-line (e.g., after a long search match) and you want to both reposition the view and get back to the beginning of indented content in one keystroke.
Example
With the cursor deep in a long line:
function veryLongFunctionName(arg1, arg2, arg3) { ← cursor here somewhere
Pressing z. centers this line in the window and snaps the cursor to function (the first non-blank character), equivalent to pressing zz then ^.
Tips
- Think of
z.aszz+^, andz<CR>aszt+^, andz-aszb+^ - Most useful in workflows where you navigate to matches mid-line (e.g., after
/pattern) and want to re-anchor your view and cursor at once - Works with a count:
3z<CR>scrolls line 3 to the top and moves to its first non-blank character