OpenWindows Advanced User's Guide

6.2 The Two Modes of vi

There are two modes of operation in vi: entry mode and command mode. You use entry mode to enter text into a file, while command mode is used to enter commands that perform specific vi functions. Command mode is the default mode for vi.

Since vi doesn't indicate which mode you're currently in, distinguishing between command mode and entry mode is probably the single greatest cause of confusion among new vi users. However, if you remember just a few basic concepts from the beginning, you should be able to avoid most of the usual "vi stress."

When you first open a vi file, it's always in command mode. Before you can enter text in the file, you must type one of the vi entry commands, such as i ("insert"), to insert text at the current cursor location, or a ("append"), to insert text after the current cursor location. (These and other vi entry commands are covered in greater detail later in this chapter.)

Whenever you want to return vi to command mode, press Esc. If you're not sure which mode vi is presently in, simply press Esc to make sure it's in command mode and continue from there. If you press Esc while vi is already in command mode, the system beeps and the screen flashes, but no harm is done.

6.2.1 Entry Mode

To enter text in the sample file paint, type the vi "insert" command i. This takes vi out of command mode and puts it into entry mode.

Now type a few short lines of text, ending every line with a Return. Characters you type appear to the left of the cursor and push any existing characters to the right. For the moment, you can correct your mistakes by backspacing and retyping a line before you press Return. Later you will learn how to edit the text you entered.

When you finish entering text in paint, press Esc to return to command mode. The cursor moves back onto the last character entered. Now you can enter more vi commands.

If vi seems to act unpredictably, make sure that you are not in "Caps Lock" mode, which would cause your entries to be all capital letters. On some systems, the F1 key (which is usually located next to the Esc key) acts as the Caps Lock. Pressing this key instead of Esc is a common error.


Note -

Occasionally you may need to instruct vi to clear or redraw the screen to eliminate, for example, extraneous system messages. To redraw the screen, enter command mode and press Ctrl-L. This is similar to the OpenWindows Refresh command.


6.2.2 Command Mode

When you open a file with vi, you are in command mode. In this mode, you can enter commands to implement a wide range of functions. Most vi commands consist of one or two letters and an optional number. Usually, there are upper and lowercase versions of commands that perform related but different functions. As an example, typing a appends the file to the right of the cursor, while typing A appends the file at the end of the line.

Most vi commands don't require that you press Return to execute them. Commands beginning with a colon (:), however, do require that you press Return after the command. Some discussions of the vi editor refer to commands preceded with a colon as a third, and uniquely separate mode of vi, last-line mode. This is because when you type the colon while in command mode, the colon and the remainder of what is typed appear on the bottom line of the screen. For the purpose of this discussion, however, all vi commands are initiated from command mode.

Commands preceded with a colon are actually ex commands. vi and ex are two separate interfaces to the same text editing program. While vi is a screen-oriented interface, ex is a line-oriented interface. The full set of ex commands is available from within vi. When you press the colon, you are actually switching to the line-oriented, ex interface. This allows you to perform many file manipulation commands without ever leaving vi. See "6.6 Using ex Commands", in this chapter, for further information.