How do I center the screen on the cursor line and also move the cursor to the first non-blank character?
Answer
z.
Explanation
Vim has two families of scroll-and-reposition commands. The familiar zz, zt, and zb commands scroll the view but leave the cursor on its current character. Their lesser-known variants — z., z<CR>, and z- — scroll the view and jump the cursor to the first non-blank character of the current line.
How it works
Each pair does the same scroll, but differs in where the cursor lands:
| Command | Scroll position | Cursor moves to |
|---|---|---|
zz |
Center | Current character (unchanged) |
z. |
Center | First non-blank character |
zt |
Top | Current character (unchanged) |
z<CR> |
Top | First non-blank character |
zb |
Bottom | Current character (unchanged) |
z- |
Bottom | First non-blank character |
Example
Given this Python file where the cursor is somewhere on the indented line:
if condition:
do_something() ← cursor is here, on the 'd'
return
Pressing zz centers the screen with the cursor still on d.
Pressing z. centers the screen AND moves the cursor to the first non-blank — the d in do_something(), which is the same in this case. But if the cursor were on _ inside the word, z. would snap it to the first non-blank character of the line (d).
Tips
z.is most useful when you want to both center the view and start at the beginning of the line's content- These commands accept a count:
5z<CR>scrolls so line 5 is at the top and moves cursor to its first non-blank - Use
zzwhen you just want to center the view without losing your cursor position