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.
q Quit; don't make any more changes.
l Last; make this change and then quit.
CTRL-E Scroll the text one line up.
CTRL-Y Scroll the text one line down.
The "from" part of the substitute command is actually a pattern. The same
kind as used for the search command. For example, this command only
substitutes "the" when it appears at the start of a line: >
:s/^the/these/
If you are substituting with a "from" or "to" part that includes a slash, you
need to put a backslash before it. A simpler way is to use another character
instead of the slash. A plus, for example: >
:s+one/two+one or two+
==============================================================================
*10.3* Command ranges
The ":substitute" command, and many other : commands, can be applied to a
selection of lines. This is called a range.
The simple form of a range is {number},{number}. For example: >
:1,5s/this/that/g
Executes the substitute command on the lines 1 to 5. Line 5 is included.
The range is always placed before the command.
A single number can be used to address one specific line: >
:54s/President/Fool/
Some commands work on the whole file when you do not specify a range. To make
them work on the current line the "." address is used. The ":write" command
works like that. Without a range, it writes the whole file. To make it write
only the current line into a file: >
:.write otherfile
The first line always has number one. How about the last line? The "$"
character is used for this. For example, to substitute in the lines from the
cursor to the end: >
:.,$s/yes/no/
The "%" range that we used before, is actually a short way to say "1,$", from
the first to the last line.
USING A PATTERN IN A RANGE
Suppose you are editing a chapter in a book, and want to replace all
occurrences of "grey" with "gray". But only in this chapter, not in the next
one. You know that only chapter boundaries have the word "Chapter" in the
first column. This command will work then: >
:?^Chapter?,/^Chapter/s=grey=gray=g
You can see a search pattern is used twice. The first "?^Chapter?" finds the
line above the current position that matches this pattern. Thus the ?pattern?
range is used to search backwards. Similarly, "/^Chapter/" is used to search
forward for the start of the next chapter.
To avoid confusion with the slashes, the "=" character was used in the
substitute command here. A slash or another character would have worked as
well.
ADD AND SUBTRACT
There is a slight error in the above command: If the title of the next chapter
had included "grey" it would be replaced as well. Maybe that's what you
wanted, but what if you didn't? Then you can specify an offset.
To search for a pattern and then use the line above it: >
/Chapter/-1
You can use any number instead of the 1. To address the second line below the
match: >
/Chapter/+2
The offsets can also be used with the other items in a range. Look at this
one: >
:.+3,$-5
This