This is part of a series of tips on using Vi. To see the complete series, click here.
When someone wants to move up or down in a file that they are editing, I often notice that they either use the arrow keys to move one line at a time, or the Page Up and Page Down keys to move a screen at a time. Although this works fine, it has the disadvantage that you have to move your hands off the home keys, which messes up your touch typing. Fortunately, vi offers commands for doing the same thing:
- Ctrl-F works like Page Down: it moves you down a page in the document, leaving the last two lines on the previous page as the top two lines of the next page to help retain context. You can remember Ctrl-F as “move Forward”.
- Ctrl-B works like Page Up: it moves you up a page in the document, leaving the top two lines on the previous page as the bottom two lines of the next page to help retain context. The mnemonic for this command is “move Backward”.
But maybe you don’t want to move a full page at a time — maybe the common 2 lines isn’t enough context. You can also scroll up and down half a page at a time:
- Ctrl-D moves you down half a page. The previous bottom line is now displayed in the middle of your screen. You can remember this with “move Down”.
- Ctrl-U moves you up half a page. The previous top line is now displayed in the middle of your screen. Remember this one with “move Up”.
Finally, maybe you just want to move the display one line up or down:
- Ctrl-E moves the display up one line, but, unlike the previous commands, it doesn’t move your cursor — your cursor stays on the same line it was on before executing Ctrl-E. Another way of thinking of this command is that it exposes a new line at the bottom of the screen, hence the mnemonic “Expose a line”.
- Ctrl-Y is the opposite of Ctrl-E: it moves the display down one line (exposes a new line at the top of the screen), and leaves your cursor in the current position. Sadly, I’ve got no easy memory trick for remembering this one — if you’ve got one, add it in the comments below!