The global and substitute commands allow you to perform complex changes to a file in a single command. Learning how to use these commands is a must for an experienced vi user.
g/pattern/cmds
The g command has two distinct phases. In the first phase,each line matching pattern in the editing buffer is marked. Next, the given command list is executed with the current line, dot(.),initially set to each marked line.
The command list consists of the remaining commands on the current input line and may continue to multiple lines by ending all but the last such line with a backslash (//). This multiple-line option will not work from within vi. You musttch to ex to do it. The vi command Q can be used to exit to ex, and the ex command vi will return you to visual mode. If cmds (or the trailing slash (/) delimiter) is omitted, each line matching pattern is displayed.
The g command itself may not appear in cmds. The options autoprint and autoindent are inhibited during a global command and the value of the report option is temporarily infinite, in deference to a report for the entire global. Finally, the context mark ( /' ) or ( `) is set to the value of the current line (.) before the global command begins and is not changed during a global command.
The following global commands, most of them substitutions, cover the most frequent uses of the global command.
g/s1/p
Prints all lines that contain the string s1.
g/s1/s//s2/
Substitutes the first occurrence of s1 on all lines that contain it with the string s2.
g/s1/s//s2/g
Substitutes all occurrences of s1 with the string s2. This includes multiple occurrences of s1 on a line.
g/s1/s//s2/gp
Works the same as the preceding example, except that in addition,all changed lines are displayed on the screen.
g/s1/s//s2/gc
Prompts you to confirm that you want to make each substitution of the string s1 with the string s2. If you enter a Y, the given substitution is made, otherwise it is not.
g/s0/s/s1/s2/g
Marks all those lines that contain the string s0, and then for those lines only, substitutes all occurrences of the string s1 with s2.
g!/pattern/cmds
Runs cmds at each line not matching pattern.
g/^/s// /g
Inserts blank spaces at the beginning of each line in a file.
s/pattern/repl/options
On each specified line, replaces the first instance of text matching the regular expression pattern by the replacement text repl. If the global indicator option character g appears, all instances on a line are substituted. If the confirm indication character c appears, before each substitution the line to be substituted is printed on the screen with the string to be substituted marked with caret (^) characters. By entering Y, you cause the substitution to be performed; any other input causes no change to take place. After an s command, the current line is the last line substituted.
v/pattern/cmds
A synonym for the global command variant g!, runs the specified cmds on each line that does not match pattern.
vim高级技巧
最新推荐文章于 2024-07-18 15:07:29 发布
href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />