vimtricks.wiki Concise Vim tricks, one at a time.

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 unchanged
  • z<CR> — redraw so current line is at top, cursor moves to first non-blank
  • zz — redraw so current line is at center, cursor column unchanged
  • z. — redraw so current line is at center, cursor moves to first non-blank
  • zb — redraw so current line is at bottom, cursor column unchanged
  • z- — 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 '{'
  • zt scrolls foo to top, cursor stays on {
  • z<CR> scrolls foo to top, cursor jumps to f in function

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: 10zt scrolls line 10 to the top.

Next

How do I get a brief visual flash to confirm what text was just yanked in Neovim?