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

How do I scroll up half a page in Vim?

Answer

<C-u>

Explanation

The <C-u> (Ctrl+u) command scrolls the window up by half a screen, moving the cursor along with it. It is one of the primary ways to navigate through a file vertically without losing your place.

How it works

  • <C-u> scrolls the viewport up by half the window height
  • The cursor moves up by the same number of lines
  • The number of lines scrolled is determined by the scroll option, which defaults to half the window height

Example

If your terminal window shows 40 lines, pressing <C-u> scrolls up by 20 lines. The cursor moves up 20 lines as well, keeping your position relative to the viewport.

Tips

  • Use <C-d> to scroll down by half a page (the counterpart to <C-u>)
  • Use <C-b> to scroll up by a full page ("backward")
  • Use <C-f> to scroll down by a full page ("forward")
  • You can change the scroll distance with :set scroll=10 to scroll 10 lines at a time
  • Use zz after scrolling to center the cursor line on the screen
  • Use zt to move the current line to the top of the screen, or zb to move it to the bottom
  • Unlike k or arrow keys, <C-u> moves through the file much faster and is the preferred way to scan through code

Next

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