Navigating in vi
In command mode, your keyboard becomes an interface tool, as opposed to a text-input tool. vi is designed for users that need access to all common commands while keeping their hands on the home keys (a-s-d-f and j-k-l-;) and reaching to nearby letters. The first basic operation to learn is moving the cursor. Most modern versions of vi allow you to use the arrow keys on the keyboard, but advanced vi users prefer the easily accessible keys under their fingertips, h-j-k-l:
- h and l represent left and right, respectively, which is intuitive because they bound the four navigation keys on the left and on the right.
- k moves the cursor up.
- j moves the cursor down.
You'll learn these keys quickly with muscle memory. To move the cursor to the first line of the three-line file you created, press k two times. The cursor now sits at the end of first line. Go ahead and use h-j-k-l to move the cursor to other locations in the file, but then bring it back to the end of line one. See Figure 5.
Figure 5. Move the cursor back to the first line of the file with the h-j-k-l directional keys
Now that your cursor is at the end of first line, you might want to move it to the beginning of the line, but you don't want to click h over and over again to get there. In command mode, vi has keyboard-based shortcuts that let you navigate rapidly to various locations in the file more quickly than you could by moving your hand over to the mouse and pointing to the location in the file you want to go to, or using arrow keys to traverse spaces one by one. The first such shortcut is 0 (zero):
- To go to the beginning of a line, press 0; your cursor jumps to that location.
- To go back to the end of the line, press $.
Go ahead and try it.
Now you can move one character at a time. You can also move to the beginning and end of each line, but those are two widely varying granularities. Another option is to navigate word by word. You can do so with the w and b keys:
- Pressing w moves forward one word.
- Pressing b moves back one word.
Try it by moving to the beginning of the first line (by pressing 0) and then move to the beginning of the word test by pressing w three times. Then, press b twice to get back to the word is.
You probably noticed that the w key and the b key set the cursor to the beginning of each word. You can also navigate to the end of each word by using the e key to move forward, or by pressing g. Press e to go backward. See Figure 6.
Figure 6. Moving the cursor word by word with w and b is a good way to quickly navigate to a word you misspelled or would like to change
Now, you can quickly move within a line but, as you deal with
larger files, it's also important to rapidly navigate from
line to line. There are a few ways to do this in vi. You can use the up and down commands (
j), or you can use the
page-down commands. Most commands in vi don't require you
to press the Ctrl key, but the
page-down commands are a couple of exceptions to this loose rule:
- Press Ctrl-u to go up a page.
- Press Ctrl-d to go down a page.
To quickly navigate to the beginning or end of a file, you can use gg or G:
- Pressing gg puts the cursor on the first line of the document.
- Pressing G puts the cursor on the last line of the document.
Additional methods specify line numbers; these are discussed in the Using numbers to preface vi commands section.