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

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

Answer

zt

Explanation

The zt command repositions the viewport so that the line where your cursor sits moves to the top of the screen, without changing your cursor position within the line. This is one of three essential scroll-anchoring commands that give you precise control over what you see while editing.

How it works

  • zt scrolls the window so the current line is at the top of the screen
  • zz scrolls so the current line is in the middle (note: different from ZZ which saves and quits)
  • zb scrolls so the current line is at the bottom of the screen
  • Your cursor stays on the same line and column — only the viewport moves

Example

You jump to a function definition with gd and land on line 250, but it's at the very bottom of the screen with no context below it:

... (lines above)
function calculateTotal(items) {   ← cursor here, at bottom of screen

Press zt and now the function signature is at the top with the full function body visible below:

function calculateTotal(items) {   ← cursor here, at top of screen
  let sum = 0;
  for (const item of items) {
    sum += item.price;
  }
  return sum;
}

Tips

  • Use zt after jumping to a search result or definition to frame the code you're about to read
  • z<CR> is similar to zt but also moves the cursor to the first non-blank character on the line
  • zb is useful when you want to see context above the current line
  • These commands pair perfectly with navigation commands like *, n, gd, and mark jumps
  • Add a count to target a different line: 50zt scrolls line 50 to the top of the screen
  • Unlike <C-e> and <C-y> which scroll one line at a time, zt/zz/zb immediately reframe the viewport around your cursor

Next

How do I edit multiple lines at once using multiple cursors in Vim?