Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
  Previous   Contents   Next 
   
 
Chapter 6

Using the vi Editor

vi (pronounced "vee-eye," short for visual display editor) is the standard SunOS text editor. vi is not window based and can be used on any kind of terminal to edit a wide range of file types.

You can type and edit text with vi, but it is not a word processor. vi does not process formatted text in the familiar manner of a commercial word processor. To produce formatted printouts, vi relies on a typesetting emulation program, such as nroff, troff, or ditroff. These programs enable you to format vi text by inserting codes that are then interpreted by the emulator.

vi uses a variety of commands, many of which have functions that overlap. This chapter provides an overview of the most essential vi commands. As you begin to use vi, you will find that it is an extremely powerful text editor, and proficiency happens with practice.


Note - The view utility is a read-only version of vi. When you open a file with view, you can use vi commands, but you cannot accidentally change the file by saving your changes.


Starting vi

The following sections describe how to start vi, type text in a file, save (write) the file, and quit vi.

Creating a File

Start vi and edit the file paint as shown in this example:

$ vi paint

If paint already exists, vi opens the existing file. If this is a new file, vi creates it. For the purposes of this example, paint should be a new file.

The vi editing screen appears in a moment:

Figure 6-1 vi Editing Screen

The cursor appears in the upper left corner of the screen. Blank lines are indicated by a vertical series of tildes (~).

Note that you can also start vi without specifying a file name by just typing vi. You can then name the file later when you exit vi.

Status Line

The last line of the screen, called the status line, shows the name of the file and the number of lines and characters in the file. When you create a new file, as is the case with the example, the status line indicates that it is a new file.

Two Modes of vi

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.

Ending a Session

When you edit a file in vi, your changes are not made directly to the file. Instead, they are applied to a copy of the file that vi creates in a temporary memory space that is called the buffer. The permanent disk copy of the file is modified only when you write (save) the contents of the buffer.

This arrangement is both positive and negative. A positive feature is that you can quit a file and discard all the changes that you have made during an editing session, leaving the disk copy intact. The negative feature is that you could lose the (unsaved) contents of the work buffer if the system crashes. People on remote terminals that are connected by phone lines are especially vulnerable to unplanned interruptions.

The best policy is to save your work frequently, especially when you are making substantive changes.


Caution - Although it's possible to run multiple, simultaneous vi sessions on one file, it is not a good idea. Great confusion could result when you try to determine which changes have been written to the file and which changes have been overwritten from a simultaneous session.


Saving Changes and Quitting vi

vi is rich in substantively synonymous commands that control the save of the buffer contents to a file and the exit from vi. These commands give you the option of saving, saving-and-quitting, or quitting-without-saving.

Saving

Save the contents of the buffer (write the buffer to the file on disk) by typing:

:w

Press Return.

Saving and Quitting

Save and quit by typing:

:wq

Press Return. Alternatively, type ZZ.

Note that the command ZZ is neither preceded by a colon nor followed by Return.

Quitting Without Saving

When you've made no changes to a file and want to quit, type:

:q

Press Return. If you have made changes, vi does not let you quit with :q. Instead, it displays the following message.
No write since last change (:quit! overrides)

If you do not want to save your changes, type:

:q!

Press Return.

Printing a File

After you quit a vi file, you can print the file with the following command:

$ lp filename

In this example, filename is the name of the vi file to be printed. This command prints the file to your default printer. The file is printed without any formatting, line for line, just as it appears on the screen. See Chapter 8, Using Printers for more information on printer commands.

Basic vi Commands

The following sections explain the following categories of vi commands.

  • Moving around in a file

  • Inserting text

  • Changing and substituting text

  • Undoing changes to text

  • Deleting text

  • Checking your spelling

  • Formatting your file output

  • Repeating commands

 
 
 
  Previous   Contents   Next