5. The Command and Insert Modes

There are two main modes in VI: command and insert. To that we can add the "ex" mode, called up with ":". Cursor mode is the default mode when starting vi, it's also the top-level mode. To go back to it when in any other mode, press ESC a few times (once is actually enough for most command modes).

5.1. Cursor motions

There are tons of ways to move the cursor around, so I'll just give the basic ones:

5.2. Repeating commands

In order to repeat a command several times, you may specify the number of repeats and then the command.(eg: 10h will move the cursor 10 characters to the left)

5.3. Inserting Text

Now that you can move the cursor around, let's see how you can type text:

BE CAREFUL:

  1. When in insert mode, you can delete only the text you've just insered.To delete other parts of the file, get out of insert mode (ESC) and go toDeleting Text

    Note

    Vim specific:

    If you add the following command to your ~/.vimrc or type it while running vim you will be able to backspace through the entire buffer, not just the text entered during the current "input session". :set backspace=2

  2. When you delete text that has just been inserted, see that the letters aren't erased from the screen. Instead the cursor just moves backwards and what you type will overwrite what you had "deleted". Just get used to it.

    Note

    Vim specific:

    This occured in original vi, and apparently in some modern clones (it was probably originally due to lack of processing power). In vim this isn't a problem -- all the characters you delete will disappear immediately, except perhaps when being used over a slow dialup line, but then the problem is the dialup line and not vi(m).

Most common insertion commands, all the following commands will get you in insert mode, but in different ways:

All those commands have their counterparts in capital letters:

Note

N.B.: insertion (as well as most commands) can be combined with a "multiplier", in the same way than cursor commands:

10a(type your text...) will repeat 10 times the command a. So vi will append the text you've just typed in ten times.

5.4. Deleting Text

The delete command, as many others can be mixed with cursor commands, so that all text is deleted between the current cursor position and the new cursor position.

Deletion commands:

Note

Deletion can be combined with a "multiplier", in the same way as cursor commands: 10dd will delete 10 lines

Note

Deleted text will be stored in the 'yank' buffer, replacing the previous one. (see Yanking and Pasting)

5.5. Yanking and Pasting

Copy and paste in vi is very powerful. We'll concentrate on single-buffer yanking, but be informed that, just like emacs, you can copy text into many buffers, although it is not automated.

Yanking text means 'copy'. The corresponding command is y and its syntax is exactly that same as delete's:

Yanking commands:

WARNING: entering insert mode will erase the yank buffer.

Note

Vim specific:

Vim doesn't erase the yank buffer when you enter insert mode.

Pasting commands:

There is only one pasting command: p , which means paste text in yank buffer after current cursor position.There is also its counterpart P for pasting before cursor.