vim高级技巧

7 篇文章 0 订阅
href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值