start page | rating of books | rating of authors | reviews | copyrights

Linux in a NutshellLinux in a NutshellSearch this book

Chapter 11. The vi Editor

Contents:

Review of vi Operations
vi Command-Line Options
ex Command-Line Options
Movement Commands
Edit Commands
Saving and Exiting
Accessing Multiple Files
Window Commands
Interacting with the Shell
Macros
Miscellaneous Commands
Alphabetical List of Keys in Command Mode
Syntax of ex Commands
Alphabetical Summary of ex Commands
vi Configuration

vi is the classic screen-editing program for Unix. A number of enhanced versions exist, including nvi, vim, vile, and elvis. On Linux, the vi command is usually a link to one of these programs. The Emacs editor, covered in Chapter 10, has several vi modes that allow you to use the same commands covered in this chapter.

The vi editor operates in two modes, command mode and insert mode. The dual mode makes vi an attractive editor for users who separate text entry from editing. For users who edit as they type, the modeless editing of emacs can be more comfortable.

vi is based on an older line editor called ex. A user can invoke powerful editing capabilities within vi by typing a colon (:), entering an ex command, and pressing the Return key. Furthermore, you can place ex commands in a startup file called ~/.exrc, which vi reads at the beginning of your editing session. Because ex commands are still an important part of vi, they are also described in this chapter.

One of the most common versions of vi found on Linux systems is Bram Moolenaar's Vi IMproved, or vim. On some Linux distributions, vim is the default version of vi and runs when you invoke vi. vim changes some of the basic features of vi, most notoriously changing the undo key to support multiple levels of undo. While seasoned users of vi find vim's changes disturbing, those new to vi find vim's extensive features attractive.

Fully documenting vim is beyond the scope of this chapter, but we do cover some of its most commonly used options and features. Beyond what we cover here, vim offers enhanced support to programmers through an integrated build and debugging process, syntax highlighting, extended ctags support, and support for Perl and Python, as well as GUI fonts and menus, function key mapping, independent mapping for each mode, and more. Fortunately, vim comes with a powerful help program you can use to learn more about the things we just couldn't fit into this chapter.

For more information, see the O'Reilly book Learning the vi Editor.

11.1. Review of vi Operations

This section provides a review of the following:

11.1.1. 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 Linux shell

  • Save or exit the current version of the file

11.1.2. Insert Mode

In insert mode, you can enter new text in the file. Press the Esc or Ctrl-[ keys 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 (must be followed by a movement command)

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
Replace character under cursor

R
Begin overwriting text

s
Substitute a character

S
Substitute entire line

11.1.3. 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 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 × m.

An object can represent any of the following text blocks:

Word
Includes characters up to a space or punctuation mark. A capitalized object is a variant form that recognizes only blank spaces.

Sentence
Extends to ., !, or ? followed by two spaces.

Paragraph
Extends to next blank line or nroff/troff paragraph macro (defined by para= option).

Section
Extends to next nroff/troff section heading (defined by sect= option).

11.1.3.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 into temporary buffer (for future pasting)

y]]
Copy up to the next section into temporary buffer (for future pasting)

11.1.4. 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

!
Pipe the text indicated by a subsequent movement command through the following shell command, and replace the text with the output of the shell 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.