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 from edit mode is the simplest task in vi there is only one way to do it.
To switch back to command mode:
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.
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:
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:
Figure 6.5 compares the results of these first two methods .
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:
To enter edit mode and add text above the cursor:
Table 6.1 summarizes the most common ways to switch from command mode to edit mode in vi .
W HERE TO A DD N EW T EXT
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
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: