Learning the vi Editor/Basic tasks

Learning the vi Editor: Getting acquaintedBasic tasksMaking your work easierAdvanced tasksDetailsVi clones (VimBasic navigationModesTips and TricksUseful things for programmers to knowEnhancing VimVim on WindowsVimL Script language, Vile, BB vi)vi Reference

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

edit

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

edit

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-[).

Exercise

edit

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

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

Command mode

edit

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

Moving around

edit

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.

Note
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.


Exercise

edit

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

edit
  1. Use the method in the previous section.
  2. Press <i>, then enter The quick fox jumps over the dog normally.
  3. Press <Escape>, then press <h> until the cursor is at the letter "f" of "fox". Press <i>, and then type "brown ".
  4. Press <Escape>, then press <l> until the cursor is at the letter "d". Press <i>, and then type "lazy ".
  5. Press <Escape> again, then type :quit!.

More on movement

edit

Using h, j, k, and l is ok, but vi understands more than rows and columns. These are some commands that move by text objects:

  • w moves forward to the beginning of the next word.
  • b moves backwards to the beginning of the previous word.
  • ( and ) move by sentences, either backward or forward.
  • { and } move by paragraphs.

Deleting things

edit

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 x key deletes one character
  • pressing dw deletes one word.
  • pressing dd deletes one line

Exercise

edit

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: Sad I been here, I wouldnt ever ever leave.
  2. Change the word "Sad" to "Had".
  3. Add an apostrophe after "wouldn".
  4. Delete the extra "ever".
  5. Delete the line.

Solution

edit
  1. Type the text normally. (You should already be familiar with entering insert mode and leaving it.)
  2. Enter command mode, use h to move to the start of the line, and then press x to delete the "S". Press i to enter insert mode, insert the "H", then leave insert mode by pressing Escape.
  3. Now position the cursor on the t, and press i 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 dw to delete the word.
  5. Type dd to remove the entire line.
edit


Learning the vi Editor: Getting acquaintedBasic tasksMaking your work easierAdvanced tasksDetailsVi clones (VimBasic navigationModesTips and TricksUseful things for programmers to knowEnhancing VimVim on WindowsVimL Script language, Vile, BB vi)vi Reference