You should right now be editing a file. If not, return to the main page and create a file to edit. If a verse from Lord of the Rings does not tickle your fancy, you are welcome to create or edit any other text file.
You will notice that the first character in the file is highlighted. This is the cursor, and the cursor is always positioned on one character in the file. We will first learn to move this cursor.
The keys for moving the cursor are h j k l. If you are an avid video-game player, you may have already come across these short-cuts:
Remember that going down in a file is the most common operation, so J which sits under the index finger when you are touch typing (you can touch type, right?) when your fingers are at rest on the home row.
Practice moving around the document. See what happens if you go to the end of a line that is either longer or shorter than the previous line and see what happens when you down.
Next, hold down one of the direction keys. At first it will only move one character in the given direction, but a split second later, it begins to move in that direction at a rate of approximately 20 characters per second.
If there are no more characters in the direction you are going (you are on the first line and press k or the last line and press j or press h when you are on the first character of a line or l when you are at the last character on a line, a bell should sound.
Suppose you want to delete a character. Move the cursor on top of that character and then press x. Move around the document and delete a few characters from the text file. What happens to the position of the cursor if you delete a character that is not the last character in a line? What happens if the character is the last character? Can you get x to delete the entire line, not just the characters in that line?
As before, if you hold down the x key, it will at first delete the character the cursor is on top of, but after a split second, it will continue deleting characters as if you were repeatedly striking x.
If you are on an empty line and press x, there is nothing to delete, so a bell should sound.
If you press X, the character before the cursor is deleted.
Next, press u to undo the last edit you take. Press u multiple times, and you will see the characters you deleted reappear. If there are no edits to undo, a bell should sound.
Like before, if you hold down the u key, at first it will only perform one undo, but then it will start performing undos at a rate of approximately 20 undos per second.
Suppose you undid an action you actually wanted to perform. In this case, you can redo that edit by pressing Ctrl-R. If there are no edits to be redone, a bell should sound.
Suppose you want to
You could press jjjj or xxxxx or uuuuuuuuuuuuu, but this would become tedious. Instead, you can just press 4j, 5x or 13u.
Note that xxx is three separate edits, and so if you wanted to undo all three edits, you would need to press uuu. The edits 3x or 4X are considered a single edits, and so these edits can be undone with a single u.
To move to the start of a line, type 0 (zero) and to move to the last character in a line, type $ (or Shift-4).
At this point, you're probably wondering: this is all fine and good, but how do you insert characters? By default, when you enter vi, you are in command mode, and every letter performs some type of edit or operation. If you want to actually type something, you must go into insert mode.
Move your cursor to a particular character and then hit the key a. You are now in insert mode and all the keys you type now, including the Enter key to generate a new line, will add text to the file. At the bottom of the screen, you will see the text -- INSERT --. On that last line, you will see a pair of numbers separated by a comma: the first is the line number and the second is the offset into the line.
In order to return to command mode, type Ctrl-[. Once you are again in command mode, you can once more move around or do other edits. All the characters you entered while in insert mode is considered a single edit, so if you press u, all of the characters you just entered are gone.
In some versions of vi, when you are insert mode, the cursor will change from highlighting an entire character to a pipe (|) that sits between two characters. This clearly indicates where the next character you type will be inserted.
Now, before you try this, try to deduce what will happen if you, for example, enter 5a, enter some text, and then press Ctrl-[. Having given it some thought, try it out and see if your deduction is correct.
Return to command mode and move the cursor to the first character in a given line. What happens if you press a? As you may suspect, there are times you may wish to insert text before a given character. Thus, we look at our next key.
If in command mode, you have the cursor over a character and the you press i, you will again go into insert mode with -- INSERT -- appearing on the last line, but you will now start entering text before the character on which the cursor was resting.
To return to command mode, you simply press Ctrl-[.
We have now described command mode and insert mode. A third mode is escape mode. If you are in command mode and type a :, you enter escape mode. One the last line of the page, there appears a line started with a : and the cursor appears next to that colon. You can now enter many more complex commands.
In command mode, go to escape mode by typing : and then enter a small number, like 3 or 5, and then press Enter. You will see that you jump to that line. If you enter a number larger than the number of lines in the file, you will simply jump to the last line in file.
Up to now, we have not made any changes to the actual text in the file. To write all the changes you have made to the hard derive, type :w and press Enter. The : enters you into escape mode, and the command w means write.
Similarly, to quit, type :q and press Enter. If you have not made any changes to the file, vi will quit. If you have made changes, however, you will see a warning notice: E37: No write since last change (add ! to override). This tells you how to quit vi without saving the changes you have made since the last write: type :q! and press Enter.
If you type :wq and press Enter, vi will write the file and then quit.
You are now welcome to go to Lesson 2.