|
Operators and objectsLast revision August 2, 2004
The format for operator commands is:
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:
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 dx 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:
General change operator cThe 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 yThe 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 syntaxRepeating 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$.
|