home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: 7.4 Summary of Commands by Name Chapter 8 Next: 8.2 Movement Commands
 

8. The vi Editor

This chapter presents the following topics:

  • 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

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:

  • Command-line syntax

  • vi modes

  • Syntax of vi commands

  • Status-line commands

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, Unix Commands , 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, The ex Editor , 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:

a Append after cursor.
A Append at end of line.
c Begin change operation.
C Change to end of line.
i Insert before cursor.
I Insert at beginning of line.
o Open a line below current line.
O Open a line above current line.
R Begin overwriting text.
s Substitute a character.
S Substitute 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:

c Begin a change.
d Begin a deletion.
y Begin 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.

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

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

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

8.1.4.1 Examples

2cw Change the next two words.
d} Delete up to next paragraph.
d^ Delete back to beginning of line.
5yy Copy the next five lines.
y]] Copy up to the next section.

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.


Previous: 7.4 Summary of Commands by Name UNIX in a Nutshell: System V Edition Next: 8.2 Movement Commands
7.4 Summary of Commands by Name Book Index 8.2 Movement Commands

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System