What is the difference between zt and z-Enter when scrolling the current line to the top of the screen?
Answer
z<CR> and zt
Explanation
Both zt and z<CR> scroll the view so that the current line lands at the top of the screen, but they differ in one small but important way: z<CR> also moves the cursor to the first non-blank character of the line, while zt leaves the cursor exactly where it is.
The same distinction applies to the other scroll-to-position pairs:
| Keeps column | Moves to first non-blank |
|---|---|
zt |
z<CR> |
zz |
z. |
zb |
z- |
How it works
zt— redraw so current line is at top, cursor column unchangedz<CR>— redraw so current line is at top, cursor moves to first non-blankzz— redraw so current line is at center, cursor column unchangedz.— redraw so current line is at center, cursor moves to first non-blankzb— redraw so current line is at bottom, cursor column unchangedz-— redraw so current line is at bottom, cursor moves to first non-blank
Example
With the cursor mid-line on some indented code:
function foo() { ← cursor is on the '{'
ztscrollsfooto top, cursor stays on{z<CR>scrollsfooto top, cursor jumps tofinfunction
Tips
- The
z.variant (center + first non-blank) is useful when you want to both reposition the view and snap the cursor to the beginning of the line in one keystroke. - All six commands accept a count prefix to scroll a different line to that position:
10ztscrolls line 10 to the top.