It makes sense to be overly careful when using a search and replace command.
It sometimes happens that what you get is not what you expect.
You can undo any search and replacement command by
, provided that the command was intended for the most recent
edit you made.
But you don't always catch undesired changes until it is too late
to undo them.
Another way to protect your edited file is to save the file with
before performing a global replacement.
Then at least you can quit the file without saving your edits and
go back to where you were before the change was made.
You can also read the
previous version of the buffer back in with
It's wise to be cautious and know exactly what is going to be
changed in your file.
If you'd like to see what the search turns up
and confirm each replacement before it is made,
option (for confirm)
at the end of the substitute command:
This command will display the entire line where the string
has been located, and the string will be marked by a series
of carets (
copyists at his school
If you want to make the replacement, you must enter
If you don't want to make a change, simply press
this can be used for invitations, signs, and menus.
The combination of the vi
(repeat last search) and dot (
(repeat last command), is also an extraordinarily useful and
quick way to page through a file and make repetitive changes that
you may not want to make globally. So, for example, if your editor has
told you that
you're using which
when you should be using that
, you can spot-check
every occurrence of which
, changing only those that
|Search for which
|Change to that
||Repeat change (if appropriate).
This often turns out to be faster than using a global
substitution with confirmation.
[It also lets you see other lines near the text you're checking. -JP