In the Replace command Vim article introduced, substitute
the command syntax is: :[range]s[ubstitute]/{pattern}/{string}/[flags]
.
[flags]
Is the optional flag, commonly used include g
, c
, n
, e
and so on. Wherein the flag g
so that substitute
the command can be modified in a line all matches, not just at the first match; flag c
can confirm or reject every modification; flag n
so Vim replacement operation is not performed, but only show the current statistics and matching the number of substitute command; flag e
dedicated to masking error; flag &
to indicate the Vim reusing a substitute command flag used.
An operating range, Vim substitute command
For ease of understanding, can be thought of as a two-dimensional plane of the entire file, along the x-axis increasing to the right character, and the text lines with the y axis growth. By default, the substitute
command only acts on the current line, and will only modify the first place match.
How to expand to the entire range will be replaced by the x and y axis plane surrounded with? This article next to all the words appear below the going are replaced by rolling an example step by step instructions.
When the going gets tough, the tough get going.
If you are going through hell, keep going.
Use substitute
command, the simplest way is to provide the pattern to be matched {pattern}
replacement string as well {string}
. Therefore, the first attempt to execute a command in Vim command line mode:
:s/going/rolling
After executing the above command, you can be found at the first Vim has been going replaced with Rolling , but the text did not match the contents of other substitution occurs.
In order to substitute the entire command to act on the horizontal axis , it required the use of flags g
.
g
It appears to be global (global) meaning, easily mistaken g
flag is used to implement the replacement operation over the entire file scope, but in fact, g
represent only the current a full line range. Because Vim directly inherited from the "line editor ed", for the line editor, the global scope is a line, which is the g
origin of the flag command.
Continue to try to execute a command in Vim command line mode:
:s/going/rolling/g
After executing the command, all occurrences found in the current line of going all been replaced with rolling , but in addition to the current line outside the file to another location, there are still some match has not been modified.
How can I control substitute
command throughout the file of the vertical axis to perform on it?
The answer is to use the subsititute
command format [range]
to specify a range of execution. If you need to execute commands in each row replace the entire file, just in the substitute
beginning of the command of the prefix %
, with regard to range
the specific use of recommended reading Vim operating range, the range of the file description .
This time, try to execute the following command in Vim command line mode and found that the entire document is going to replace the word has become a new word in Rolling .
:%s/going/rolling/g
To summarize the above content is: If you want to find and replace a partial match in the current file, it is necessary to explicitly indicate substitute
the command throughout the x -axis and y the specific context of the implementation of the shaft, i.e., through the flag g
processing characters while the horizontal axis using range
the specified processing range longitudinal axis.
Second, manual control of each replacement operation
Sometimes, before the implementation of specific replace operation, you may need to observe where the match before deciding whether to be replaced.
For example, in a file, assuming that the word going appeared a total of 10 times, the need for specific context, going third appearance should not be replaced with rolling, if used directly :%s/going/rolling/g
it will replace all going to be rolling this obviously does not meet the requirements.
In order to accomplish the above object of the operation, it is necessary to use a flag c
to control substitute
the behavior of the command, the command is to be executed:
:%s/going/rolling/gc
Increase flag c
after, Vim will before replacing each match result at all tips "are replaced by rolling?" For the operator to manually confirm, press the y
button to confirm once modified, can also press the n
button to skip last modified. Whatever the result of the selection, Vim will decide to perform input and move the cursor to the next match at the prompt again.
In addition to the election y
confirmed the election n
skip outside, you can also type q
quit the replacement process, the use of a
replacement and where all matches after indicating where Vim without having to manually confirm, can also be used l
after replacing the current Vim direct indication of the match exit replacement process.
The following table summarizes all the answers and use replacement for Vim tips given.
answer | use |
---|---|
y |
Replace match here |
n |
Ignore match here |
q |
Exit replacement process |
l |
After replacing the matching here exit (l expressed last) |
a |
After replacing all matches here (a represents all) |
<Ctrl-e> |
Scroll up |
<Ctrl-y> |
Scroll down |