Keystrokes
Results
u
With a screen editor you can
scroll the page.
move the cursor.
u undoes the last command and restores the deleted line.
U, the uppercase version of u, undoes all edits on a single line, as long as the cursor
remains on that line . Once you move off a line, you can no longer use U.
Note that you can undo your last undo with u, toggling between two versions of text.
u will also undo U, and U will undo any changes to a line, including those made with u.
A tip: the fact that u can undo itself leads to a nifty way to get around
in a file. If you ever want to get back to the site of your last edit, simply
undo it. You will pop back to the appropriate line. When you undo the
undo, youll stay on that line.
Vim lets you use CTRL-R to redo an undone operation. Combined with infinite
undo, you can move backward and forward through the history of changes to your file.
See the section Undoing Undos on page 296 for more information.
Simple Edits | 29
More Ways to Insert Text
You have inserted text before the cursor with the sequence:
i text to be inserted ESC
Youve also inserted text after the cursor with the a command. Here are some other
insert commands for inserting text at different positions relative to the cursor:
A
Append text to end of current line.
I
Insert text at beginning of line.
o (lowercase letter o)
Open blank line below cursor for text.
O (uppercase letter o)
Open blank line above cursor for text.
s
Delete character at cursor and substitute text.
S
Delete line and substitute text.
R
Overstrike existing characters with new characters.
All of these commands place you in insert mode. After inserting text, remember to press
ESC to return to command mode.
A (append) and I (insert) save you from having to move your cursor to the end or
beginning of the line before invoking insert mode. (The A command saves one keystroke
over $a. Although one keystroke might not seem like much of a saving, the more adept
and impatientan editor you become, the more keystrokes you will want to omit.)
o and O (open) save you from having to insert a carriage return. You can type these
commands from anywhere within the line.
s and S (substitute) allow you to delete a character or a whole line and replace the
deletion with any amount of new text. s is the equivalent of the two-stroke command
c SPACE , and S is the same as cc. One of the best uses for s is to change one character
to several characters.
R (large replace) is useful when you want to start changing text, but you dont know
exactly how much. For example, instead of guessing whether to say 3cw or 4cw, just type
R and then enter your replacement text.
30 | Chapter 2:Simple Editing
Numeric Arguments for Insert Commands
Except for o and O, the insert commands just listed (plus i and a) take numeric prefixes.
With numeric prefixes, you might use the commands i, I, a, and A to insert a row of
underlines or alternating characters. For example, typing 50i* ESC inserts 50 asterisks,
and typing 25a*- ESC appends 50 characters (25 pairs of asterisk and hyphen). Its
better to repeat only a small string of characters.§
With a numeric prefix, r replaces that number of characters with a repeated instance
of a single character. For example, in C or C++ code, to change || to &&, you would
place the cursor on the first pipe character and type 2r&.
You can use a numeric prefix with S to substitute several lines. Its quicker and more
flexible, though, to use c with a movement command.
A good case for using the s command with a numeric prefix is when you want to change
a few characters in the middle of a word. Typing r wouldnt be correct, and typing cw
would change too much text. Using s with a numeric prefix is usually the same as typing
R.
There are other combinations of commands that work naturally together. For example,
ea is useful
for appending new text to the end of a word. It helps to train yourself to
recognize such useful combinations so that they become automatic.
Joining Two Lines with J
Sometimes while editing a file you end up with a series of short lines that are
J difficult to scan. When you want to merge two lines into one, position the cursor
anywhere on the first line, and press J to join the two lines.
Suppose your file practice reads:
With a
screen editor
you can
scroll the page, move the cursor
Keystrokes
Results
J
With a screen editor
you can
scroll the page, move the cursor
J joins the line the cursor is on with the line below.
.
With a screen editor you can
scroll the page, move the cursor
Repeat the last command (J) with the . to join the next line with the current line.
§ Very old versions of vi have difficulty repeating the insertion of more than one lines worth of text.
Joining Two Lines with J | 31
Using a numeric argument with J joins that number of consecutive lines. In the example
here, you could have joined three lines by using the command 3J.
Problem Checklist
When you type commands, text jumps around on the screen and nothing works the