About vi s Two Modes

About vi's Two Modes

The most important thing to learn about vi is that it has two modes: command mode and edit mode .

A good way to start thinking about vi 's two modes is to compare vi with any graphical user interface (GUI) word processor you have used, but imagine that the only way to move the pointer or select text is by typing commands. That presents a problem: How does the software know when you are typing a command, and when you are typing text to go into the file?

vi handles the problem of distinguishing commands from text by its two modes. In edit mode (also sometimes called insert mode ), anything you type is inserted into the file. In fact, the only thing you can do in edit mode is enter text and move around inside the file using the arrow keys. In some older versions of vi , you can't even move around in edit mode, but happily Mac OS X comes with a version that does allow you to use the arrow keys while in edit mode.

In command mode, anything you type is interpreted as a command. Saving and quitting involve using commands, and so does moving to the beginning or end of the file, or selecting a range of text to replace, or searching for text, and so on. Anything you would use the mouse for in a GUI word processor is accomplished from command mode in vi .

You must be in command mode in order to save a file, quit, undo, search and replace, copy and paste, and perform all similar functions.

Typing the same thing in the two different modes will have very different results. This is probably the most difficult thing to get used to in vi , along with the fact that you cannot use the mouse (all moving of the cursor in both modes is done from the keyboard.)

Switching to command mode

Switching to command mode from edit mode is the simplest task in vi there is only one way to do it.

To switch back to command mode:

  • Press .

    The key is located at the upper-left corner on most Macintosh keyboards. This is the only way to switch back to command mode from edit mode. If you are already in command mode, you will get a beep.


  • If you aren't sure whether you are in command mode, go ahead and press . Once you hear the beep, you'll know you are in command mode.

Switching to edit mode

When you first start vi , you are always in command mode. In order to enter text, you must switch to edit mode.

Before switching to edit mode, you should see where exactly the cursor is in the file and think about where you want the next text to be inserted (just as with the insertion bar in GUI word processors).

There are several ways to switch to edit mode, and the method you choose determines where the new text will go. For instance, if you want the new text to be inserted after (to the right of) the current cursor position, then you will choose one particular method of switching to edit mode. If you want the new text to be inserted before (to the left of) the cursor, then you will choose a different method.

All of the one-letter commands shown here for switching to edit mode take effect immediatelythat is, you do not press after any of them.

To enter edit mode and add text before the cursor:

  • i

    Do not press . The i command (for insert ) lets you edit to the left of wherever the cursor was when you typed i .


  • This is usually the way you want to switch to edit mode, but be carefulwhen you want to add text to the end of a line, use the method shown next.

To enter edit mode and add text after the cursor:

  • a

    The a command (for append ) switches you to edit mode and lets you add text after (to the right of) the cursor position. (Compare with the i command, which lets you insert text before the cursor position.)

Figure 6.5 compares the results of these first two methods .

Figure 6.5. Text is inserted either before or after the initial cursor position, depending on how you switch to edit mode.


  • The a command is the one to use when you want to add text to the end of a line.

To enter edit mode and add text below the current line:

  • o

    The o command (lowercase letter o ) opens a new line below the one you were on. New text is entered on that line ( Figure 6.6 ).

    Figure 6.6. Using the o command to switch to edit mode opens a new line below the one you were on.

To enter edit mode and add text above the cursor:

  • O

    The uppercase O command does the same thing as the lowercase o , except it opens a line above the one you were on ( Figure 6.7 ).

    Figure 6.7. Using the uppercase O command to switch to edit mode opens a new line above the one you were on.

Table 6.1 summarizes the most common ways to switch from command mode to edit mode in vi .

Table 6.1. Switching to Edit Mode in vi



Before the cursor


After the cursor


Below the current line


Above the current line


Word Wrap in vi

An excellent feature of vi is its ability to deal with very long lines of text, far wider than the 80 columns that are the default display in a Terminal window.

You have to press at the end of each line in vi , unlike what you do with paragraph based word processors you are probably used to.

You can tell vi to automatically insert a return character when you get within x columns of the right edge of the Terminal window. To do this, make sure you are in command mode (press ) and then type

:set wm=5

for auto returns within five columns of the edge. You can change that number to anything convenient . If you want that option to always be on, put the following line in a file in your home directory called .exrc:

set wm=5

Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net