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 . To move forward to the beginning of the next word, press . To move back to the beginning of the previous word, press .
The capital letter of each command has a similar behavior. Use
to insert at the beginning of a line. Use to start the append from the end of the line. To move “real” words, use to move forward and to 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
, will move me to the ‘-‘. Pressing again, we move to the and then to the . If we are on the ‘s’ and press , 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 . If we want to move to the left we press . If you think about it, this make sense since is on the left end of these four characters. To move right, press . Again, this makes sense as the 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
(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,
will move us forward four words or 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
we press , 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 cursor position down 12 lines, we would input or press Enter.
and ). So, to delete everything from your current location to line 83, we would input . (Note that delete begins with d.) Or, to change everything from the current