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 editor
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
Entering text is the most basic task an editor can do! From command mode (in which the editor starts), press  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-[).

Exercise
Let's have an exercise:


 * 1) Start the editor.
 * 2) Enter insert mode.
 * 3) Type some text
 * 4) Return to command mode.
 * 5) Quit the editor.

Solution

 * 1) We can start the editor as in the previous section.
 * 2) Since the editor starts in command mode, we must press the  key before we can start typing.
 * 3) You can make some text up yourself!
 * 4) Press the  key.
 * 5) 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

Command mode
Command mode allows you to perform many useful tasks within vi.

Moving around
Say you have been writing for some time, and have forgotten something. Pressing, erasing previous work is not the best solution! We would like to move around the document freely, moving the cursor.

Exercise
You can repeat this exercise with your own sentences. Make sure you are proficient doing this before you continue.


 * 1) Start the editor.
 * 2) Enter the text: "The quick fox jumps over the dog"
 * 3) Insert the word "brown" between "quick" and "fox".
 * 4) Insert the word "lazy" between "the" and "dog".
 * 5) Quit the editor.

Solution

 * 1) Use the method in the previous section.
 * 2) Press, then enter   normally.
 * 3) Press, then press  until the cursor is at the letter "f" of "fox". Press , and then type "brown ".
 * 4) Press, then press  until the cursor is at the letter "d". Press , and then type "lazy ".
 * 5) Press  again, then type.

More on movement
Using,  ,  , and   is ok, but vi understands more than rows and columns. These are some commands that move by text objects:
 * moves forward to the beginning of the next word.
 * moves backwards to the beginning of the previous word.
 * and  move by sentences, either backward or forward.
 * and  move by paragraphs.

Deleting things
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:
 * the  key deletes one character
 * pressing  deletes one word.
 * pressing  deletes one line

Exercise
From now on, we will omit the steps for you to start and quit the editor – you should be already familiar with those.


 * 1) Enter the following text:
 * 2) Change the word "Sad" to "Had".
 * 3) Add an apostrophe after "wouldn".
 * 4) Delete the extra "ever".
 * 5) Delete the line.

Solution

 * 1) Type the text normally. (You should already be familiar with entering insert mode and leaving it.)
 * 2) Enter command mode, use   to move to the start of the line, and then press   to delete the "S". Press   to enter insert mode, insert the "H", then leave insert mode by pressing Escape.
 * 3) Now position the cursor on the t, and press   to enter insert mode, type the " ' ". Leave insert mode by pressing escape.
 * 4) Position the cursor over the first "e" in the word "ever" (choose whichever one you like). Type   to delete the word.
 * 5) Type   to remove the entire line.