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

How do I redraw the screen with the cursor line at center or bottom and move to the first non-blank character?

Answer

z. / z-

Explanation

Vim has scroll-positioning commands that come in two flavors: those that leave the cursor in the current column (zz, zt, zb) and those that also move the cursor to the first non-blank character of the line (z., z<CR>, z-). This subtle distinction matters when your cursor is sitting in the middle of indented code.

How it works

Command Cursor line position Cursor column
zz Center of screen Unchanged
z. Center of screen First non-blank
zt Top of screen Unchanged
z<CR> Top of screen First non-blank
zb Bottom of screen Unchanged
z- Bottom of screen First non-blank

Example

Given this Python code with the cursor somewhere inside a method:

    def process(self):
        result = self._calculate()
       ^ cursor here (column 7)

Pressing z. redraws with the line centered and snaps the cursor to column 4 (the first non-blank r in result):

        result = self._calculate()
    ^ cursor jumps to first non-blank

Tips

  • Use z. instead of zz when you want to also normalize your cursor position to the start of the line's content
  • z<CR> is the top-of-screen equivalent — useful for quickly scrolling a function header into view and landing on the code
  • These commands are especially handy in heavily indented code where you want to see what you're working on without hunting for the logical start of the line

Next

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