This article originates from http://blog.csdn.net/shuangde800
This article is a note taken in the study of "Using VI Editor, Lamb & Robbins".
article content:Basic global substitution confirm substitution context-related substitution
This article provides a convention for:The $ + command represents the normal bash command line: + command representation in Vim's ex (Command) mode, press "Ctrl" + ":" Enter
------------------------------------------------------------------------------------------------
basic syntax for replacing commands
In global substitution, the ex editor checks each row in the file with the specified character pattern. Finds the location of the pattern in all rows, and ex replaces the pattern with the new string.
The global replacement is actually using two ex commands that are: G(Global overall) and: S(Switch substitution).
The syntax for the substitution command is as follows:: S/old/new/This will modify the first occurrence of the current pattern old to new. /(italic bar) is the delimiter between different parts of the command (not written when the diagonal bar is the last character of the line)
Following this form of substitution command:: s/old/new/gChange each occurrence of the current row old to new, not just the first old of the row. : The s command allows the replacement string to be followed by an option, and g in the above syntax represents the global (the G option affects each pattern in a row and does not confuse it with the G command that affects all rows in the file)
By adding an address prefix to the: s command, you can extend its range to multiple lines. For example, the following command changes each occurrence of 50 rows to 100 lines to new:: s/old/new/g
The following command changes each occurrence of the old in the entire file to new:: 1, $s/old/new/gWe know that% is equivalent to 1,$, so the uplink command can also write this::% s/old/new/g
Confirm Replacement
Using search substitution commands can sometimes be an error, and you get unwanted results. So it's a good idea to be careful and confirm what you need to modify in your file.
At the end of the Replace command, addC(Confirm is used for confirmation) and prompts and confirms before replacing each old:: 1 s/old/new/gcA prompt for replace with hehe (y/n/a/q/l/^e/^y) will appear? Y replaces, N does not replace, a replaces all, Q discards, L replaces the first and enters insert mode, ^e and ^y are prompts you to use Ctrl+e or ctrl+y to scroll the screen.
context-sensitive substitution
In addition to replacing one word (or phrase) directly with another, there is a slightly more complex global substitution syntax. These syntaxes can be searched for a pattern, and once a row containing the pattern is found, it can be replaced with a different string of patterns, which we call the context-related substitution.
The syntax format is as follows:: g/pattern/s/old/new/gWill replace all old in the line containing pattern with new The first G represents the command executed on all the lines of the file, patterns identify the rows to which the substitution occurs. On lines that contain pattern patterns, the ex replaces the old (s) with the new. The last g indicates that all substitutions are made on the line.
If the pattern used for searching is the same as the pattern you want to modify, you do not have to repeat it:: g/string/s//new/gEquivalent to: g/string/s/string/new/gAlso note:: g/string/s//new/gis also equivalent to:% s/string/new/gYou can lose a few characters in the second way