Vim is largely a keyboard-controlled text editor. You need to press the keys to scroll through a file instead of using the luxury of a mouse scroll wheel (though a mouse can also be used). To make Vim scrolling easier, in this guide, I will be exploring how to page up and page down in Vim using different methods.
Note: The instructions mentioned in this guide are performed on Linux (Ubuntu 22.04). Since Vim is cross-platform, the key bindings mentioned in this tutorial apply to all operating systems.
Page Size in Vim
In Vim, a page size is equal to the number of visible lines in the current window of the terminal. Resizing the terminal window also changes the page size.
Page Up and Down
Enable the NORMAL mode and use the following commands to page up and down in Vim.
- ctrl+f: To page down by one page
- ctrl+b: To page up (back) by one page
- ctrl+d: To page down by half page
- ctrl+u: To page up by half page
On pressing the ctrl+f the page will go up and move the cursor on lastline-1. For example, in the following file, the current on-page visible lines are 20. The cursor is on line 1, after pressing the ctrl+f, lines 1 to 18 will be replaced by lines 19 to 37 in the current buffer, and so on.
The same is true in the case of page-down (ctrl+b) but in the opposite direction.
The commands mentioned above are a bit stiff and may not provide you with a smooth scrolling experience. I prefer to use the other built-in commands.
- ctrl y: To move the screen up by one line
- ctrl e: To move the screen down by one line
As you can see in the following GIF, the scrolling is much easier.
Some other quick keys to scroll through the page in Vim are listed below.
- zz: To bring the line under the cursor to the center
- zt: To bring the line under the cursor to the top
- zb: To bring the line under the cursor line to the bottom
Using Vim Navigation Keys to Page Up and Down
Vim has built-in key bindings h, j, k, and l which can be used for page scrolling. The keys j and k are for vertical scrolling.
- j: To move the cursor up by one line
- k: To move the cursor down by one line
Page Up and Down Using Marks
Page navigation is also possible using the built-in bookmark functionality of Vim. Set a mark at any place in the file using the m command followed by a character {a-z}. You can set 26 marks in a file using the small case letters.
Use the square bracket ([) with the backtick (`) to navigate the marks up and down, resulting in on-page navigation. For instance, use ]` to jump to the next mark and [` to the previous.
Page Up and Down Synchronously in Multiple Windows
Many developers prefer to use a multi-window layout in Vim. In many situations, it becomes desirable that both windows must scroll synchronously. This functionality can be achieved using the scrollbind command.
Open Vim in multi-window mode, and use the :set scrollbind command in the current window.
Now, press ctrl+w to switch to the next window and again use the above-mentioned command.
The following animated GIF shows that the page’s movement is occurring simultaneously.
To learn more about scroll binding, use the :help scrollbind command.
Move to Different Positions on the Page
To move the cursor to the different positions on a page or window, use the following keys:
- shift+h: To the top of the page
- shift+m: To the middle of the page
- shift+l: To the end of the page
How to Page Up/down in Vim without Moving the Cursor
Due to the development structure of the Vim, it is not possible to page up or down without moving the cursor. However, shortcut keys like zz, zt, and zb can be used to move the window relative to the cursor.
Conclusion
Vim provides various shortcut keys to navigate the page. To page up and down, Vim has default keys, the ctrl+f and ctrl+b. However, in many situations, these keys do not provide the desired operation, such as scrolling to a specific position after page up or page down. In such a case, the ctrl+y and ctrl+e keys are used, which scroll the page by one line.