UNIX in a Nutshell: System V Edition

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

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 section presents the following topics:

8.1 Review of Vi Operations

This subsection provides a review of the following:

For more information on vi, refer to the Nutshell Handbook Learning the vi Editor.

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 Section 2 for more information on command-line options for vi.

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, then the operator is the same as the object: 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 X m.

An object can represent any of the following text blocks:

wordIncludes characters up to a space or punctuation mark. A capitalized object is a variant form that recognizes only blank spaces.
sentenceIs up to ., !, ? followed by two spaces.
paragraphIs up to next blank line or paragraph macro defined by para= option.
sectionIs up to next section heading defined by sect= option.

8.1.4.1 Examples

2cwChange the next two words.
d}Delete up to next paragraph.
d^Delete back to beginning of line.
5yyCopy 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 the following 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 NameUNIX in a Nutshell: System V EditionNext: 8.2 Movement Commands
7.4 Summary of Commands by NameBook Index8.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