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

How do I scroll the screen so the current line is at the top or bottom?

Answer

zt / zb

Explanation

The zt and zb commands scroll the viewport so the current cursor line appears at the top or bottom of the screen respectively, without moving the cursor. Together with zz (center), these give you complete control over your view.

The trio

Command Effect
zz Scroll current line to center of screen
zt Scroll current line to top of screen
zb Scroll current line to bottom of screen

When to use each

  • zt: You're about to read downward — put the current line at top to maximize visible content below
  • zb: You're about to read upward — put the current line at bottom to see more context above
  • zz: General purpose — put current line in context with surrounding code

With line offsets

z<CR>    " Like zt, but also moves cursor to first non-blank
z.       " Like zz, but also moves cursor to first non-blank
z-       " Like zb, but also moves cursor to first non-blank

Scrolloff setting

:set scrolloff=5    " Always keep 5 lines visible above/below cursor
:set scrolloff=999  " Always keep cursor centered (like permanent zz)

Tips

  • zt is particularly useful after jumping to a function definition — it puts the function at the top so you can read the entire body
  • These commands work well after search (/pattern): jump to match, then zt to frame it
  • None of these change the cursor position in the buffer — only the viewport scrolls
  • Combine with H (top of screen), M (middle), and L (bottom) for cursor positioning

Next

How do I always access my last yanked text regardless of deletes?