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

How do I resize the current window to an exact number of lines from the command line?

Answer

z{height}<CR>

Explanation

The z{height}<CR> command simultaneously resizes the current window to exactly {height} lines, scrolls so the current line sits at the top of the window, and moves the cursor to the first non-blank character of that line. It is a lesser-known variant of the z scroll family that gives you pixel-precise window height control without reaching for :resize.

How it works

  • z begins a scroll/window command
  • {height} is the number of lines you want the window to be (e.g., 20)
  • <CR> (Enter) completes the command and triggers the resize
  • The cursor lands on the first non-blank character of the current line
  • The current line becomes the topmost visible line in the window

Example

With two horizontal splits open, place the cursor in the top split and type:

z20<CR>

The top split is now exactly 20 lines tall. The cursor moves to the first non-blank character of the current line, which is scrolled to the top of that split.

Compare with the related commands:

zz      → center current line, do NOT resize
zt      → put current line at top, do NOT resize
:resize 20  → resize to 20 lines, do NOT scroll or move cursor

Tips

  • Combine with <C-w>= to rebalance splits after manual resizing
  • Useful in a mapping: nnoremap <leader>z z25<CR> to set a preferred height for your main editing window
  • Works in both horizontal splits and the only window (sets how many rows are visible when wrap is off)
  • z<CR> with no count (just Enter) is equivalent to zt — it scrolls the current line to the top without resizing

Next

How do I get just the filename without its path or extension to use in a command?