BELUG > vi > previous

Entering Text

vi has two basic modes of operation: command mode and text insert mode. How to switch back and forth between them is probably the most confusing thing about vi for beginners. But it is actually very simple, and once you get used to it you might also find it quite efficient.

Command mode is the default mode when a file (existing or new) is opened. (This is the opposite of most text and word processors and therefore may seem counter-intuitive.) Because every file opens initially in command mode, you can not immediately begin typing text. That is, everything that is typed on the keyboard is interpreted by vi to be a command.

Examples of the many types of commands that you can perform on a file while in command mode are (1) switching to text insert mode, (2) moving the cursor around the file, (3) deleting characters or lines, (4) transposing characters, (6) changing case, (6) appending the contents of the file to another (closed) file, (7) setting vi options, (8) saving the file to disk and (9) closing the file and quitting vi.

The other mode, text insert mode, is also referred to as simply "insert mode" or "input mode." It is used for entering text into the buffer memory (and simultaneously onto the screen). In this mode everything that is typed on the keyboard is added to the text and does not become a command (although you can perform some command operations in text mode with vi clones).

The most common way to switch from command mode to the input mode is to use the i (which stands for "insert" or "input") command. This is accomplished by simply typing the letter i while in command mode. Now you are ready to start typing text.

Unlike word processors and even most word editors, there is no automatic word wrap in the traditional version of vi (although you will notice it in some clones). New lines are started by pressing ENTER.

When you are finished typing text or you want to perform some other operation such as moving to a different position in the text or deleting some of it, hit ESC in order to return to the command mode.

It is not easy to determine vi's current mode. But this is not really a problem because, if in doubt, just press ESC and you will know that you are in the command mode (this will put you in command mode if you were not already in it).

Once you have typed some text, you can use the four basic commands for moving the cursor around the text. These commands enable you to go to any desired location in order to modify the text, including making insertions and deletions. The four basic cursor positioning commands are:

h    move cursor one character to left
j    move cursor one line down
k    move cursor one line up
l    move cursor one character to right

Each of these commands can be either used by itself or modified by typing an integer in front of it to indicate the number of characters or lines to move. For example, typing (in command mode, of course)


will move the cursor down three lines. Or typing 2h will move it two characters to the left.

These commands can be repeated by holding the key down. If you attempt an impossible movement, such as pressing k when the cursor is on the top line, the screen might flash or a beeping sound might be made (depending on how your computer is set up).

The cursor can be moved directly to any desired line by using the G command preceded by the line number. For example, typing


moves the cursor to the fifth line from the top of the text. Just typing G without any number moves the cursor to the final line of text.

When you switch from command mode to input mode with the i command and then start typing text, each character you type is placed to the left of the character covered by the cursor. This causes the character covered by the cursor as well as everything to its right to be shifted to the right.

There will be times when you will want to place a character to the right of the character under the cursor. This is particularly useful when the cursor is over the last character in a line and you want to append the line. To do this, simply use the a (lower case "a," which stands for "append") command instead of the i command to switch from command mode into insert mode.

After you have saved a file that you have created or modified using vi, you might want to verify that its contents are really what you had intended. One way to do this is to use cat, the Unix concatenation utility. (No, this has no relationship to the popular domesticated animal whose name has the same spelling). For example, type:

cat /home/john/fruit/lemon

This causes the contents of the file "lemon" in the /home/john/fruit/ directory to be displayed on the screen in a read-only form.

Of course, if your current directory is already /home/john/fruit/, then you only need to type:

cat lemon