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
scrolloption, 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=10to scroll 10 lines at a time - Use
zzafter scrolling to center the cursor line on the screen - Use
ztto move the current line to the top of the screen, orzbto move it to the bottom - Unlike
kor arrow keys,<C-u>moves through the file much faster and is the preferred way to scan through code