C the same effect as combining c with the special end-of-line indicator $ (c$).
Simple Edits | 21
The commands cc and C are really shortcuts for other commands, so they dont follow
the general form of vi commands. Youll see other shortcuts when we discuss the delete
and yank commands.
Characters
One other replacement edit is given by the r command. r replaces a single
r
character with another single character. You do not have to press ESC to return
to command mode after making the edit. There is a misspelling in the line below:
Pith a screen editor you can scroll the page,
Only one letter needs to be corrected. You dont want to use cw in this instance because
you would have to retype the entire word. Use r to replace a single character at the
cursor:
Keystrokes
Results
rW
With a screen editor you can scroll the page,
Give the replace command r, followed by the replacement character W .
Substituting text
Suppose you want to change just a few characters, and not a whole word. The
s substitute command (s), by itself, replaces a single character. With a preceding
count, you can replace that many characters. As with the change command (c), the last
character of the text will be marked with a $ so that you can see how much text will be
changed.
The S command, as is usually the case with uppercase commands, lets you change
S whole lines. In contrast to the C command, which changes the rest of the line from
the current cursor position, the S command deletes the entire line, no matter where the
cursor is. vi puts you in insert mode at the beginning of the line. A preceding count
replaces that many lines.
Both s and S put you in insert mode; when you are finished entering new text, press
ESC .
The R command, like its lowercase counterpart, replaces text. The difference is
R that R simply enters overstrike mode. The characters you type replace whats on
the screen, character by character, until you type ESC . You can overstrike a maximum
of only one line; as you type ENTER , vi will open a new line, effectively putting you
into insert mode.
22 | Chapter 2:Simple Editing
Changing Case
Changing the case of a letter is a special form of replacement. The tilde (~) com-
~ mand will change a lowercase letter to uppercase or an uppercase letter to low-
ercase. Position the cursor on the letter whose case you want to change, and type a ~.
The case of the letter will change, and the cursor will move to the next character.
In older versions of vi, you cannot specify a numeric prefix or text object for the ~ to
affect. Modern versions do allow a numeric prefix.
If you want to change the case of more than one line at a time, you must filter the text
through a Unix command such as tr, as described in Chapter 7.
Deleting Text
You can also delete any text in your file with the delete command, d. Like the
d change command, the delete command requires a text object (the amount of text
to be operated on). You can delete by word (dw), by line (dd and D), or by other movement
commands that you will learn later.
With all deletions, you move to where you want the edit to take place, then give the
delete command (d) and the text object, such as w for word.
Words
Suppose you have the following text in the file:
d
w
Screen editors are are very popular,
since they allowed you to make
changes as you read through a file.
with the cursor positioned as shown. You want to delete one are
in the first line:
Keystrokes
Results
2w
Screen editors are are very popular,
since they allowed you to make
changes as you read through a file.
Move the cursor to where you want the edit to begin ( are ).
dw
Screen editors are very popular,
since they allowed you to make
changes as you read through a file.
Give the delete word command (dw) to delete the word are .
dw deletes a word beginning where the cursor is positioned. Notice that the space fol-
lowing the word is deleted.
Simple Edits | 23
dw can also be used to delete a portion of a word. In this example:
since they allowed you to make
you want to delete the ed from the end of allowed .
Keystrokes
Results
dw
since they allowyou to make
Give the delete word command (dw) to delete the word, beginning with the position of the
cursor.
dw always deletes the space before the next word on a line, but we dont want to do that
in this example. To retain the space between words, use de, which deletes only to the
end of a word. Typing dE deletes to the end of a word, including punctuation.
You can also delete backward (db) or to the end or beginning of a line (d$ or d0).
Lines
The dd command deletes the entire line that the cursor is on. dd will not
d
d delete part of a line. Like its complement, cc, dd is a special command. Using
the same text as in the previous example, with the cursor positioned on the first line as
shown here:
Screen editors are very popular,
since they allow you to make
changes as you read through a file.
you can delete the first two lines: