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

How do I scroll the screen while also jumping to the first non-blank character of the current line?

Answer

z<CR> and z. and z-

Explanation

Vim has two sets of scroll-and-position commands: zt/zz/zb (which reposition the screen but keep the cursor column intact) and z<CR>/z./z- (which reposition the screen and move the cursor to the first non-blank character). Knowing both lets you precisely control where you land after scrolling.

How it works

Command Screen position Cursor column
zt Current line → top Unchanged
z<CR> Current line → top First non-blank
zz Current line → middle Unchanged
z. Current line → middle First non-blank
zb Current line → bottom Unchanged
z- Current line → bottom First non-blank

The z<CR>, z., and z- variants are the lesser-known cousins of zt, zz, and zb. They perform the same scrolling but additionally move the cursor to the first non-blank character, similar to how pressing ^ would.

Example

Given a function definition indented with spaces:

    function computeResult() {   ← cursor here (column 5)
        return 42;
    }

Pressing z<CR> scrolls this line to the top of the screen and moves the cursor to f in function (first non-blank). Pressing zt would scroll identically but leave the cursor at column 5.

Tips

  • Use z<CR> instead of zt^ when you want both effects in one keystroke
  • All six commands accept a count prefix: 3z<CR> scrolls line 3 to the top and moves cursor there
  • The same count behavior applies to zt/zz/zb — a prefix line number means "scroll so line N is at the target position"

Next

How do I highlight all occurrences of a yanked word without typing a search pattern?