Operators and objects



Last revision August 2, 2004

Table of Contents:
  1. Editor choices on Unix
  2. Characteristics, advantages, and disadvantages of vi
  3. Basic text editing operations in vi
  4. Regular expressions
  5. File searching with grep
  6. More about regular expressions
  7. Intermediate text editing with vi
  8. Vi Quick Reference

The format for operator commands is:

    [repeat count] operator [number] object

repeat count is an optional integer giving how many times to repeat entire command - effect is to multiply number of objects. Allowed to maintain consistency with other commands that allow repeat counts. Defaults to one if not given.

operator is a single letter command that performs an action on the text described by the object. The operators (to be described below) are:

     d    deletion operator

     c    change operator

     y    yank (copy) operator

     !    filter operator

number is an optional integer giving the number of objects to operate on. It defaults to one object if not specified.

object is a command specifying a direction and amount of text to affect. These are the same as the commands that move the cursor or search through the file. If any of these objects is used by itself (without an operator), it moves the cursor or screen display to a new section of the file. When used as object for an operator, they indicate that all text between the current cursor position and the position the cursor would take from this object, is to be affected by the operator.

In other words, imagine that each of these objects represents throwing a stone to the location defined by it. Everything from when you are (where the cursor is) to where the stone lands (the destination of the object) is affected by the operator. Commonly used objects include:

space bar forward one space
w forward to beginning of next word or punctuation mark
W forward to beginning of next word (skip over punctuation)
e forward to end of current word
E forward to end of current word, including punctuation.
b backward to beginning of previous word or punctuation
B backward to beginning of previous word (skip over punctuation)
$ forward to end of current line
0 backward to beginning of current line
) and ( forward and backward to next sentence (defined by punctuation).
} and { forward and backward to next paragraph (defined by blank line).
G forward to end of file; or with optional leading line number, e.g., 15G, forward or backward, as appropriate, to that specific line in the file.
H backward to beginning of line on top of current screen
M backward or forward, as appropriate, to end of line on middle of current screen
L forward to end of line on bottom of current screen
/pattern/ forward up to (not including) the first line that contains a string that matches pattern. Use ? instead of / to reverse the direction of the search.

Notice that most of the cursor and window positioning commands can be thought of as just the use of a "null operator" with one of the objects given above. The "operation" performed on the object is simply to move the cursor to the next occurrence. Repeat counts in front of the object work for most of these positioning commands, for example, 5w moves the cursor forward by five words.

General deletion operator d

x and dd are used to delete single characters and single lines, respectively. These are just special cases (aliases) for the more general delete operator d

Use d with any of the objects listed above to delete that amount of text. Examples:

dw deletes up to, but not including, the beginning of the next word
de deletes up to the end of the current word
d10G deletes up to and including line 10 in the file - will delete backwards if the cursor is already past line 10
dL deletes up to and including the last line currently on the screen.

General change operator c

The c (change) operator is the same as the d (delete) operator, except that after deleting the specified text, it immediately puts you into insert mode to add new text. The c operator can use any of the same objects as the d operator to indicate how much text is to be replaced.

If the deleted text consists of whole lines, they will be erased from the screen ("lower" lines moved up), leaving you on a new blank line in insert mode. If the deleted text is part of a line, vi does not actually erase the replaced text on screen. Instead, the cursor is placed on the first character of the text to be changed, and the last character of the text to be changed is indicated by replacing it with a dollar sign ($). You then type in the replacement text, ending it with the ESC key. You are probably going to type over the text to be replaced anyway, so why bother erasing it and then just having to show the remaining text on the line be moved over to the right as you type the replacement text.

The s (substitute) command already described is simply an alias for the c command with the space bar (single character) object.

Copy operator y

The y (yank or copy) operator is used to copy text from one area of the file to another. It is described in the section on moving lines of text.

Filter operator !

The ! (filter) operator is used to send some text from the file to another Unix program to be formatted or modified (filtered) in some way. It is described in the section on filtering text.

Some general special operator syntax

Repeating the operator name causes it to act on the entire current line. For example, dd deletes the entire current line, and cc changes the entire current line.

Capitalizing the operator usually causes it to take the rest of the line from the cursor position on as its object. For example, C is the same as c$ and D is the same as d$.

Comments or Questions?