Book HomeBook TitleSearch this book

Chapter 8. The vi Editor

Contents:

Review of vi Operations
Movement Commands
Edit Commands
Saving and Exiting
Accessing Multiple Files
Interacting with Unix
Macros
Miscellaneous Commands
Alphabetical List of Keys
Setting Up vi

This chapter presents the following topics:

vi is pronounced “vee eye.”

Besides the original Unix vi, there are a number of freely available vi clones. Both the original vi and the clones are covered in Learning the vi Editor, listed in the Bibliography.

8.1. Review of vi Operations

This section provides a review of the following:

8.1.1. Command-Line Syntax

The three most common ways of starting a vi session are:

vi file
vi +n file
vi +/pattern file

You can open file for editing, optionally at line n or at the first line matching pattern. If no file is specified, vi opens with an empty buffer. See Chapter 2, for more information on command-line options for vi.

Note that vi and ex are actually the same program; thus it is worthwhile to review the material in Chapter 9, as well, in order to become familiar with the ex command set.

8.1.2. Command Mode

Once the file is opened, you are in command mode. From command mode, you can:

  • Invoke insert mode

  • Issue editing commands

  • Move the cursor to a different position in the file

  • Invoke ex commands

  • Invoke a Unix shell

  • Save or exit the current version of the file

8.1.3. Insert Mode

In insert mode, you can enter new text in the file. Press the Escape key to exit insert mode and return to command mode. The following commands invoke insert mode:

aAppend after cursor.
AAppend at end of line.
cBegin change operation.
CChange to end of line.
iInsert before cursor.
IInsert at beginning of line.
oOpen a line below current line.
OOpen a line above current line.
RBegin overwriting text.
sSubstitute a character.
SSubstitute entire line.

8.1.4. Syntax of vi Commands

In vi, commands have the following general form:

[n] operator [m] object

The basic editing operators are:

cBegin a change.
dBegin a deletion.
yBegin a yank (or copy).

If the current line is the object of the operation, the object is the same as the operator: cc, dd, yy. Otherwise, the editing operators act on objects specified by cursor-movement commands or pattern-matching commands. n and m are the number of times the operation is performed, or the number of objects the operation is performed on. If both n and m are specified, the effect is n × m.

An object can represent any of the following text blocks:

word

Includes characters up to a whitespace character (space or tab) or punctuation mark. A capitalized object is a variant form that recognizes only whitespace.

sentenceIs up to ., !, or ?, followed by two spaces.
paragraph

Is up to next blank line or paragraph macro defined by the para= option.

sectionIs up to next section heading defined by the sect= option.

8.1.5. Status-Line Commands

Most commands are not echoed on the screen as you input them. However, the status line at the bottom of the screen is used to echo input for these commands:

/Search forward for a pattern.
?Search backward for a pattern.
:Invoke an ex command.
!

Invoke a Unix command that takes as its input an object in the buffer and replaces it with output from the command.

Commands that are input on the status line must be entered by pressing the Return key. In addition, error messages and output from the CTRL-G command are displayed on the status line.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.