"@a" to replay the recorded
commands. Finally, you can use 26 different registers. Thus you can remember
26 different command sequences to execute.
USING REGISTERS
The registers used for recording are the same ones you used for yank and
delete commands. This allows you to mix recording with other commands to
manipulate the registers.
Suppose you have recorded a few commands in register n. When you execute
this with "@n" you notice you did something wrong. You could try recording
again, but perhaps you will make another mistake. Instead, use this trick:
G Go to the end of the file.
o<Esc> Create an empty line.
"np Put the text from the n register. You now see
the commands you typed as text in the file.
{edits} Change the commands that were wrong. This is
just like editing text.
0 Go to the start of the line.
"ny$ Yank the corrected commands into the n
register.
dd Delete the scratch line.
Now you can execute the corrected commands with "@n". (If your recorded
commands include line breaks, adjust the last two items in the example to
include all the lines.)
APPENDING TO A REGISTER
So far we have used a lowercase letter for the register name. To append to a
register, use an uppercase letter.
Suppose you have recorded a command to change a word to register c. It
works properly, but you would like to add a search for the next word to
change. This can be done with: >
qC/word<Enter>q
You start with "qC", which records to the c register and appends. Thus
writing to an uppercase register name means to append to the register with
the same letter, but lowercase.
This works both with recording and with yank and delete commands. For
example, you want to collect a sequence of lines into the a register. Yank
the first line with: >
"ayy
Now move to the second line, and type: >
"Ayy
Repeat this command for all lines. The a register now contains all those
lines, in the order you yanked them.
==============================================================================
*10.2* Substitution *find-replace*
The ":substitute" command enables you to perform string replacements on a
whole range of lines. The general form of this command is as follows: >
:[range]substitute/from/to/[flags]
This command changes the "from" string to the "to" string in the lines
specified with [range]. For example, you can change "Professor" to "Teacher"
in all lines with the following command: >
:%substitute/Professor/Teacher/
<
Note:
The ":substitute" command is almost never spelled out completely.
Most of the time, people use the abbreviated version ":s". From here
on the abbreviation will be used.
The "%" before the command specifies the command works on all lines. Without
a range, ":s" only works on the current line. More about ranges in the next
section |10.3|.
By default, the ":substitute" command changes only the first occurrence on
each line. For example, the preceding command changes the line:
Professor Smith criticized Professor Johnson today. ~
to:
Teacher Smith criticized Professor Johnson today. ~
To change every occurrence on the line, you need to add the g (global) flag.
The command: >
:%s/Professor/Teacher/g
results in (starting with the original line):
Teacher Smith criticized Teacher Johnson today. ~
Other flags include p (print), which causes the ":substitute" command to print
out the last line it changes. The c (confirm) flag tells ":substitute" to ask
you for confirmation before it performs each substitution. Enter the
following: >
:%s/Professor/Teacher/c
Vim finds the first occurrence of "Professor" and displays the text it is
about to change. You get the following prompt: >
replace with Teacher? (y)es/(n)o/(a)ll/(q)uit/(l)ast/scroll up(^E)/down(^Y)
At this point, you must enter one of the following answers:
y Yes; make this change.
n No; skip this match.
a All; make this change and all remaining ones without
further confirmation.