Moving Around in vi

Moving Around in vi

Most editing and movement commands are single letters and are almost always the first letter of what they do. For example, to insert text at your current cursor position, press i. To append text, press a. To move forward to the beginning of the next word, press w. To move back to the beginning of the previous word, press b.

The capital letter of each command has a similar behavior. Use I to insert at the beginning of a line. Use A to start the append from the end of the line. To move “real” words, use W to move forward and Bto move back.

Real words are those terminated by whitespaces (space, tab, newline). Assume we wanted to move across the phrase ‘static-free bag’. If we start on the ‘s’, pressing w, will move me to the ‘-‘. Pressing w again, we move to the f and then to the b. If we are on the ‘s’ and press W, we jump immediately to the ‘b’. That is, to the next “real” word.

Movement in vi is also accomplished in other ways. Depending on your terminal type, you can use the traditional method of arrow keys to move within the file. If vi doesn’t like your terminal type, you can use the keys h-j-k-l. If we want to move to the left we press h. If you think about it, this make sense since h is on the left end of these four characters. To move right, press l. Again, this makes sense as the l is on the right end.

Movement up and down is not quite as intuitive. One of the two remaining characters (j and k) will move us up and the other will move us down. But which one moves in which direction? Unfortunately, I don’t have a very sophisticated way of remembering. If you look at the two letters physically, maybe it helps. If you imagine a line running through the middle of these characters, then you see that j hangs down below that line. Therefore, use j to move down. On the other hand, k sticks up above the middle, so we use k to move up. However, in most cases, the arrow keys will work, so you won’t need to remember. But it is nice to know them, as you can then leave your fingers on the keyboard.

As I mentioned, some keyboard types will allow you to use the arrow keys. However, you might be surprised by their behavior in input mode. This is especially true if you are used to a word processor where the arrow and other movement keys are the same all the time. The problem lies in the fact that most keyboards actually send more than one character to indicate something like a left-arrow or page-up key. The first of these is normally an escape (Esc). When you press one of these characters in input mode, the Esc is interpreted as your wish to leave input mode.

If we want to move to the first character on a line, we press 0 (zero) or ^. To move to the last character, press $. Now, these are not intuitive. However, if you think back to our discussion on regular expressions, you’ll remember that ^ (caret) represents the beginning of a line and $ (dollar sign) represents the end of a line. Although, these two characters do not necessarily have an intuitive logic, they do fit in with other commands and programs that you find on a Linux system.

We can also take advantage of the fact that vi can count as well as combine movement with this ability to count. By pressing a number before the movement command, vi will behave as if we had pressed the movement key that many times. For example, 4w will move us forward four words or 6j will move us six lines down.

If we want to move to a particular line we input the number and G. So, to move to line 43, we would press 42G, kind of like 42-Go! If instead of G we press Enter, we would move ahead that many lines. For example, if we were on line 85, pressing 42 and Enter would put us on line 127. (No, you don’t have to count lines; vi can display them for you, as we’ll see later.)

As you might have guessed, we can also use these commands in conjunction with the movement keys (all except Ctrl-u and Ctrl-d). So, to delete everything from your current location to line 83, we would input d83G. (Note that delete begins with d.) Or, to change everything from the current cursor position down 12 lines, we would input c12+ or press c12 Enter.