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

How do I scroll the current line to the top of the screen and move the cursor to the first non-blank character?

Answer

z<CR>

Explanation

While zt scrolls the current line to the top of the screen, z<CR> does the same scroll but also moves the cursor to the first non-blank character of that line. This is one of Vim's subtle but useful variant scroll commands.

How it works

Vim has paired scroll-and-reposition commands:

  • zt — scroll current line to top; cursor stays in the same column
  • z<CR> — scroll current line to top; cursor moves to first non-blank
  • zz — scroll current line to middle; cursor stays in same column
  • z. — scroll current line to middle; cursor moves to first non-blank
  • zb — scroll current line to bottom; cursor stays in same column
  • z- — scroll current line to bottom; cursor moves to first non-blank

The z<CR>, z., and z- variants are the lesser-known counterparts to zt, zz, and zb.

Example

With the cursor on column 20 of a deeply indented line:

        return some_long_variable_name;

Pressing zt scrolls the line to the top but keeps the cursor at column 20. Pressing z<CR> scrolls the same way but moves the cursor to r in return (the first non-blank character).

Tips

  • Use z<CR> when you want to start reading or editing from the beginning of the line after scrolling
  • The {count}z<CR> form first jumps to line {count}, then scrolls it to the top with cursor on first non-blank
  • These commands work the same in Vim and Neovim

Next

How do I assign a key to toggle paste mode on and off without typing :set paste each time?