How can a user switch from insert mode to command mode when using the vi editor?

VI (pronouced "vee-eye") is not the latest, greatest, more feature-packed UNIX editor. Still, it is something of a standard, as it exists on 99% of all UNIX systems. Also, some other programs out there use VI's "hjkl" cursor movement key layout.

Starting: To begin editing a file with vi, you simply type vi followed by the file name:

vi loop.pas

vi clears the screen, puts a column of "~" symbols down the left-hand side, and leaves the cursor at the top. The "~" symbols are there to indicate end-of-file. You are now in one of vi's two modes -- Command mode. Command mode is used for cursor movement, editing, retrieving and saving files, etc. If you want to type in text, you need to be in Insert mode.

To move from Command mode to Insert mode, press "i" (no quotes).
To move from Insert mode to Command mode, press "ESC" (the Escape key).

NOTE: If your terminal doesn't have an ESC key, or the ESC key doesn't work, use Ctrl-[ instead.

Pressing "i" is but one of several ways to get into insert mode, but it is the most common. The command list (below) will introduce others. Once in insert mode, you can type in your text. Press ENTER at the end of each line. Use Backspace to delete to the left of the cursor. If you need to move the cursor to another line, or make a change, or pretty much anything else, you need to press ESC to get back to Command mode first.

Command Mode Command List

Here is a list of the most common vi commands. Most anything you'd ever want to do can be done with one of these. Remember, these are only available from Command mode. ('Current line' means the line the cursor is on. ^H = Ctrl-h; case is not significant on control characters.)

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

Because 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 type 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.

Entry Mode

To type text in the sample file paint, type the vi “insert” command i. This command removes vi from 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. For information on editing text in vi, see Changing Text.

When you finish typing text in paint, press Esc to return to command mode. The cursor moves back onto the last character you typed. Now you can type 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 (usually next to the Esc key) acts as the Caps Lock. Pressing this key instead of Esc is a common error.


Note –

Occasionally you might 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.


Command Mode

When you open a file with vi, you are in command mode. In this mode, you can type commands to implement a wide range of functions. Most vi commands consist of one or two letters and an optional number. Usually, uppercase and lowercase versions of commands 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 do not 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 that are preceded with a colon as a third, and uniquely separate mode of vi, last-line mode. This mode is so named 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 that are 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 switch enables you to perform many file manipulation commands without ever leaving vi. See Using ex Commands, in this chapter, for further information.

How can we toggle between the command mode and the insert mode?

To go into INSERT mode from COMMAND mode, you type i . To go back to COMMAND mode, you type the esc key. vim starts out in COMMAND mode. Over time, you will likely spend more time in COMMAND mode than INSERT mode.

Which keys will switch vi from command mode to insert mode choose two?

The vi always starts in command mode. To enter text, you must be in insert mode. To come in insert mode you simply type i. To get out of insert mode, press the Esc key, which will put you back into command mode.