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

How do I center the current line on screen and move to the first non-blank character?

Answer

z.

Explanation

Vim has two flavors of each screen-repositioning command: one that only moves the view and one that also repositions the cursor. z. centers the current line vertically on screen and moves the cursor to the first non-blank character of that line — useful when you want both a recentered view and a clean starting position for editing.

How it works

Vim's z scroll commands come in pairs:

Command View position Cursor moves?
zz Center No
z. Center → first non-blank
zt Top No
z<CR> Top → first non-blank
zb Bottom No
z- Bottom → first non-blank

The cursor-moving variants (z., z<CR>, z-) are analogous to _ (move to first non-blank) combined with the corresponding view command.

Example

Cursor is on a deeply indented line, somewhere in the middle of screen:

                if condition {
    cursor → │     do_something()
                }

After z.:

  (top of screen)
  ...
  if condition {
      do_something()   ← line is centered; cursor jumps to `do_something`
  }
  ...
  (bottom of screen)

Tips

  • Prefer z. over zz when you want to immediately start editing at the line's content without pressing ^ or _
  • z<CR> is handy after a search jump to snap to the top of the screen and land on the first non-blank of the matched line
  • All six commands accept a count prefix: 10z. centers line 10 on screen and moves there

Next

How do I center or right-align lines of text using Vim's built-in Ex commands?