#print
Misc. Useful Features - The Outer Editor I
You may remember that we said at the outset that vi is built on top of
another, line-oriented, editor, called 'ex'. Whenever you use a colon
command (e.g. :q, :w, :set), you are really accessing this outer editor.
One of the most useful commands available in the outer editor is the
substitution command. You can find out all about 'ex' by leaving learn
with 'bye' (when you see the percent sign) and then typing 'learn editor'.
The substitution command is for those times when you want to make a change
several times in the file without having to move to each one in turn.
Suppose we wanted to change all occurances of 'masticate' to 'chew'. We'd
use this command:
:1,$s/masticate/chew/g
What does it mean? Well, ':' is just the command to move the cursor to the
bottom of the screen on the command line. '1,$' means "do this on every line
of the file from line 1 to line $ (i.e. the end)". 's/masticate/chew/' means
"if there's an instance of masticate on this line, change it to chew". The
final 'g' stands for global, meaning "do this as many times as possible on this
line". Otherwise, the s command only changes the first occurance on the line.
In place of the '1,$' we could have used any two line numbers or the
character '.', which stands for the current line. ":.,.+5s/mast/pole/g" would
change all masts to poles in the next six lines.
Type 'ready' to try out this handy command.
#user
#create Substitute
You can learn all about the substitution command when you try
'learn editor', but remember to leave your present session with 'bye'.
Once you get the hang of it (it's rather crude at first in comparison
to vi), if you want to skip to the lessons on this command, type
'again 30.1a' from within learn, and do the lessons as they are presented.
To get a real understanding of substitutions you will need to do this at
some time, but you may want to get a glimpse of them here.
The format of a substitute command is as follows:
:
s///
where is either of the form
,
or
g//
In the first case, and can be any of
1. A line number
2. '$', meaning the last line in the file
3. '.', meaning the current line
or 4. A sum of these, like .+5
The meaning of this type of address is all lines
between and .
In the second case, vi uses all lines of the file
containing the text .
is the text to look for
is the text to replace with
and is any number of the following
g, which makes the change happen to all
occurances of on the line
c, which makes vi confirm each change before
doing it
or p, which makes vi print out each changed line
as it goes
Examples:
:1,$s/masticate/chew/g
For all lines in the file, changes every occurance of
'masticate' on each line to 'chew'.
:1,$s/masticate/chew/
Same as the previous one, but only changes the first
'masticate' on each line.
:g/fundamental/s/fundamental/basic/cp
Finds every line in which the word 'fundamental' occurs
and changes it to 'basic' after first asking for
confirmation. In this mode, vi will print the line
in which the pattern occurs, underline the pattern, and
wait. If you type 'y', the change will be made. Any
other character causes the change not to be done.
Because of the 'p' at the end of the command above, vi
will print out the line each time after it makes a
substitution.
Play around with this command for a while and then try this exercise:
-----------------------------------------------------------------------------
EXERCISE
Compose and execute a command which will change every instance of
the word 'rogue' in this file to the word 'rouge'. Make sure you
even get the instance in these instructions. Then exit, saving your
changes.
-----------------------------------------------------------------------------
#create Empty
#
cat /usr/share/learn/vi/longtext >> Substitute
vi Substitute
grep rogue Substitute > Answer
#cmp Answer Empty
#succeed
You should be proud of yourself for learning this complicated command even
with very muddled explanations... Sorry...
#fail
Try it again. The kind of command you're looking for looks like this:
:1,$s/rogue/rouge/g
If you forgot the 'g' on the end, it won't work, since vi will only change
the first instance of 'rogue' on each line and there are lines with two
instances.
When the learn program types a percent sign, type 'ready' to try it again.
#next
6.3 10