Modes

vi operates in various modes, which are used to accomplish various tasks. When you first start vi, you are placed into command mode. From this point, you can issue various commands to manipulate text, move around in the file, save, quit, and mode. Editing the text is done in insert mode. You can quickly move between modes with a variety of keystrokes, which are explained below.

Command Mode

You are first placed into command mode. From this mode, you cannot directly enter text or edit what is already there. However, you can manipulate the text, search, quit, save, load new files, and more. This is intended only to be an introduction to the command mode. For a description of the various commands, see the section called vi Keys.

Probably the most often used command in command mode is changing to insert mode. This is accomplished by hitting the i key. The cursor changes shapes, and -- INSERT -- is displayed at the bottom of the screen (note that this does not happen in all clones of vi). From there, all your keystrokes are entered into the current buffer and are displayed to the screen. To get back into command mode, hit the escape key.

Command mode is also where you move around in the file. On some systems, you can use the arrow keys to move around. On other systems, you may need to use the more traditional keys of “hjkl”. Here is a simple listing of how these keys are used to move around:

hmove left one character
jmove down one character
kmove up one character
lmove right one character

Simply press a key to move. As you will see later, these keys can be combined with a number to move much more efficiently.

Many of the commands that you will use in command mode begin with a colon. For example, quitting is :q, as discussed earlier. The colon simply indicates that it is a command, while the “q” tells vi to quit. Other commands are an optional number, followed by a letter. These commands do not have a colon before them, and are generally used to manipulate the text.

For example, deleting one line from a file is accomplished by hitting dd. This will remove the line that the cursor is on. Issuing the command 4dd would tell vi to remove the line that the cursor is on and the three after that. In general, the number tells vi how many times to perform the command.

You can combine a number with the movement keys to move around several characters at a time. For example, 10k would move up ten lines on the screen.

Command mode can also be used to cut and paste, insert text, and read other files into the current buffer. Copying text is accomplished with the y key (y stands for yank). Copying the current line is done by typing yy, and this can be prefixed with a number to yank more lines. Then, move to the location for the copy and hit p. The text is pasted on the line after the current one.

Cutting text is done by typing dd, and p can be used to paste the cut text back into the file. Reading in text from another file is a simple procedure. Just type :r, followed by a space and the file name that contains the text to be inserted. The file's contents will be pasted into the current buffer on the line after the cursor. More sophisticated vi clones even contain filename completion similar to the shell's.

The final use that will be covered is searching. Command mode allows for simple searching, as well as complicated search-and-replace commands that make use of a powerful version of regular expressions. A complete discussion of regular expressions is beyond the scope of this chapter, so this section will only cover simple means of searching.

A simple search is accomplished by hitting the / key, followed by the text that you are searching for. vi will search forward from the cursor to the end of the file for a match, stopping when it finds one. Note that inexact matches will cause vi to stop as well. For example, a search for “the” will cause vi to stop on “then”, “therefore”, and so on. This is because all of those words do match “the”, but only at the beginning.

After vi has found the first match, you can continue on to the next match simply by hitting the / key followed by enter. You can also search backwards through the file by replacing the slash with the ? key . For example, searching backwards through the file for “the” would be accomplished by typing ?the.

Insert Mode

Inserting and replacing text is accomplished in insert mode. As previously discussed, you can get into insert mode by hitting i from command mode. Then, all text that you type is entered into the current buffer. Hitting the escape key takes you back into command mode.

Replacing text is accomplished in several ways. From command mode, hitting r will allow you to replace the one character underneath the cursor. Just type the new character and it will replace the one under the cursor. You will then be immediately placed back into command mode. Hitting R allows you to replace as many characters as you'd like. To get out of this replacement mode, just hit escape to go back into command mode.

There is yet another way to toggle between insertion and replacement. Hitting the insert key from command mode will take you into insert mode. Once you are in insert mode, the keyboard's Insert key serves as a toggle between insert and replace. Hitting it once will allow you to replace. Hitting it once more will once again allow you to insert text.