Learning the vi Editor/Basic tasks
Now that we know how to invoke the editor and quit it, we can get acquainted with how to use the editor.
Alternatively, you can use the ViM tutor which comes with many modern vim distributions. It contains, essentially the same information as the text below. You can invoke the tutor by entering vimtutor at your shell.
Vi is a modal editorEdit
The vi editor can do two things:
- accept a command, such as deleting a line
- accept text, written by you
In the vi editor, each of these tasks is achieved by putting the editor into a particular mode of operation (normally just called a mode). When you wish to give vi a command, you enter command mode, and when you want to enter text, you enter insert mode. We'll cover how to do this below.
It is important to set the correct mode before you begin writing, but this is simple to do. When you first start vi, it is automatically in command mode.
Entering text is the most basic task an editor can do! From command mode (in which the editor starts), press
i to enter insert mode, and you can begin typing. You can use the backspace key to correct mistakes you make. If you make a mistake after a few sentences, leave these errors for now, we will look at correcting them later. To leave insert mode once you're done typing, and return to command mode, press the Escape key on your keyboard (or type Control-[).
Let's have an exercise:
- Start the editor.
- Enter insert mode.
- Type some text
- Return to command mode.
- Quit the editor.
- We can start the editor as in the previous section.
- Since the editor starts in command mode, we must press the <i> key before we can start typing.
- You can make some text up yourself!
- Press the <Escape> key.
- If you want to quit the editor, you need to be in command mode. Since pressing Escape puts you in command mode, you can just use the method in the previous section to exit: type :q!
Command mode allows you to perform many useful tasks within vi.
Say you have been writing for some time, and have forgotten something. Pressing <Backspace>, erasing previous work is not the best solution! We would like to move around the document freely, moving the cursor.
We can move around in the editor by first entering command mode, and then using the <h>, <j>, <k>, and <l> keys.
- your arrow keys may be set up to work, and you can use them if you like, but for proficiency and for advanced work later, you should learn to use the letter keys.
- The <h> key, in command mode, moves the cursor one character left.
- The <j> key, in command mode, moves the cursor one character down.
- The <k> key, in command mode, moves the cursor one character up.
- The <l> key, in command mode, moves the cursor one character right.
If you have trouble remembering this, keep in mind that <h> is leftmost, the letter <j>goes down below the line, the letter <k>pokes up above the line, and the <l> key is rightmost. (J also resembles an arrow pointing downward, if you squint a bit.)
After you have moved the cursor using those keys, you can enter insert mode again by pressing <i>. When you do this, you insert text at the cursor, inserting text between the character to the left of the cursor and the current position of the cursor. Let's practice this in an exercise.
You can repeat this exercise with your own sentences. Make sure you are proficient doing this before you continue.
- Start the editor.
- Enter the text: "The quick fox jumps over the dog"
- Insert the word "brown" between "quick" and "fox".
- Insert the word "lazy" between "the" and "dog".
- Quit the editor.
- Use the method in the previous section.
- Press <i>, then enter
The quick fox jumps over the dognormally.
- Press <Escape>, then press <h> until the cursor is at the letter "f" of "fox". Press <i>, and then type "brown ".
- Press <Escape>, then press <l> until the cursor is at the letter "d". Press <i>, and then type "lazy ".
- Press <Escape> again, then type :quit!.
More on movementEdit
l is ok, but vi understands more than rows and columns. These are some commands that move by text objects:
wmoves forward to the beginning of the next word.
bmoves backwards to the beginning of the previous word.
)move by sentences, either backward or forward.
}move by paragraphs.
If you have made a mistake after a few lines, for instance, pressing Backspace until you have erased the mistake and starting again isn't always the best solution. We need a method of deleting mistakes that happen in the normal course of editing.
vi allows you several methods of deleting text, based on how much you want to remove. Now that you are familiar with moving around, once you've moved the cursor to where your error is:
xkey deletes one character
dwdeletes one word.
dddeletes one line
From now on, we will omit the steps for you to start and quit the editor – you should be already familiar with those.
- Enter the following text:
Sad I been here, I wouldnt ever ever leave.
- Change the word "Sad" to "Had".
- Add an apostrophe after "wouldn".
- Delete the extra "ever".
- Delete the line.
- Type the text normally. (You should already be familiar with entering insert mode and leaving it.)
- Enter command mode, use
hto move to the start of the line, and then press
xto delete the "S". Press
ito enter insert mode, insert the "H", then leave insert mode by pressing Escape.
- Now position the cursor on the t, and press
ito enter insert mode, type the " ' ". Leave insert mode by pressing escape.
- Position the cursor over the first "e" in the word "ever" (choose whichever one you like). Type
dwto delete the word.
ddto remove the entire line.