Talk about Vim substitute Replace command

In the Replace command Vim article introduced, substitutethe command syntax is: :[range]s[ubstitute]/{pattern}/{string}/[flags].

[flags]Is the optional flag, commonly used include g, c, n, eand so on. Wherein the flag gso that substitutethe command can be modified in a line all matches, not just at the first match; flag ccan confirm or reject every modification; flag nso Vim replacement operation is not performed, but only show the current statistics and matching the number of substitute command; flag ededicated 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 substitutecommand only acts on the current line, and will only modify the first place match.

Vim replacement

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 substitutecommand, 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.

gIt appears to be global (global) meaning, easily mistaken gflag is used to implement the replacement operation over the entire file scope, but in fact, grepresent 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 gorigin 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 substitutecommand throughout the file of the vertical axis to perform on it?

The answer is to use the subsititutecommand format [range]to specify a range of execution. If you need to execute commands in each row replace the entire file, just in the substitutebeginning of the command of the prefix %, with regard to rangethe 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 substitutethe command throughout the x -axis and y the specific context of the implementation of the shaft, i.e., through the flag gprocessing characters while the horizontal axis using rangethe 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/git 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 cto control substitutethe behavior of the command, the command is to be executed:

:%s/going/rolling/gc

Increase flag cafter, Vim will before replacing each match result at all tips "are replaced by rolling?" For the operator to manually confirm, press the ybutton to confirm once modified, can also press the nbutton 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 yconfirmed the election nskip outside, you can also type qquit the replacement process, the use of areplacement and where all matches after indicating where Vim without having to manually confirm, can also be used lafter 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

vim-substitute

Guess you like

Origin blog.51cto.com/14168089/2403607