with the cursor positioned as shown. To insert With a screen editor at the beginning of
the sentence, enter the following:
Keystrokes
Results
2k
you can scroll
the page, move the cursor, delete
lines, and insert characters.
Move the cursor up two lines with the k command, to the line where you want to
make the insertion.
iWith a
With a you can scroll
the page, move the cursor, delete
lines, and insert characters.
Press i to enter insert mode and begin inserting text.
screen editor
With a screen editor you can scroll
ESC
the page, move the cursor, delete
lines, and insert characters.
Finish inserting text, and press ESC to end the insert and return to command mode.
Appending Text
You can append text at any place in your file with
the append command, a. This
a works in almost the same way as i, except that text is inserted after the cursor
rather than before the cursor. You may have noticed that when you press i to enter
insert mode, the cursor doesnt move until after you enter some text. By contrast, when
you press a to enter insert mode, the cursor moves one space to the right. When you
enter text, it appears after the original cursor position.
Changing Text
You can replace any text in your file with the change command, c. To tell c how
c
Simple Edits | 19
much text to change, you combine c with a movement command. In this way, a move-
ment command serves as a text object for the c command to affect. For example, c can
be used to change text from the cursor:
cw
To the end of a word
c2b
Back two words
c$
To the end of line
c0
To the beginning of line
After issuing a change command, you can replace the identified text with any amount
of new text, with no characters at all, with one word, or with hundreds of lines. c, like
i and a, leaves you in insert mode until you press the ESC key.
When the change affects only the current line, vi marks the end of the text that will be
changed with a $, so that you can see what part of the line is affected. (See the example
for cw, next.)
Words
To change a word, combine the c (change) command with w for word. You
c
w can replace a word (cw) with a longer or shorter word (or any amount of
text). cw can be thought of as delete the word marked and insert new text until ESC
is pressed.
Suppose you have the following line in your file practice:
With an editor you can scroll the page,
and want to change an to a screen . You need to change only one word:
Keystrokes
Results
w
With an editor you can scroll the page,
Move with w to the place you want the edit to begin.
cw
With a$ editor you can scroll the page,
Give the change word command. The end of the text to be changed will be marked with a $
(dollar sign).
a screen
With a screen editor you can scroll the page,
Type in the replacement text, and then press ESC to return to command mode.
cw also works on a portion of a word. For example, to change spelling to spelled , you
can position the cursor on the i , type cw, then type ed , and finish with ESC .
20 | Chapter 2:Simple Editing
General Form of vi Commands
In the change commands weve mentioned up to this point, you may have noticed the
following pattern:
( command )( text object )
command is the change command c, and text object is a movement command (you dont
type the parentheses). But c is not the only command that requires a text object. The
d command (delete) and the y command (yank) follow this pattern as well.
Remember also that movement commands take numeric arguments, so numbers can
be added to the text objects of c, d, and y commands. For example, d2w and 2dw are
commands to delete two words. With this in mind, you can see that most vi commands
follow a general pattern:
( command )( number )( text object )
or the equivalent form:
( number )( command )( text object )
Heres how this works. number and command are optional. Without them, you simply
have a movement command. If you add a number , you have a multiple movement. On
the other hand, combine a command (c, d, or y) with a text object to get an editing
command.
When you realize how many combinations are possible in this way, vi becomes a
powerful editor indeed!
Lines
To replace the entire current line, use the special change command, cc. cc
c
c changes an entire line, replacing that line with any amount of text entered
before pressing ESC . It doesnt matter where the cursor is located on the line; cc re-
places
the entire line of text.
A command like cw works differently from a command like cc. In using cw, the old text
remains until you type over it, and any old text that is left over (up to the $) goes away
when you press ESC . In using cc, though, the old text is wiped out first, leaving you a
blank line on which to insert text.
The type over approach happens with any change command that affects less than a
whole line, whereas the blank line approach happens with any change command that
affects one or more lines.
C replaces characters from the current cursor position to the end of the line. It has