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
ztscrolls the window so the current line is at the top of the screenzzscrolls so the current line is in the middle (note: different fromZZwhich saves and quits)zbscrolls 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
ztafter jumping to a search result or definition to frame the code you're about to read z<CR>is similar toztbut also moves the cursor to the first non-blank character on the linezbis 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:
50ztscrolls line 50 to the top of the screen - Unlike
<C-e>and<C-y>which scroll one line at a time,zt/zz/zbimmediately reframe the viewport around your cursor