VI编辑器中查找/替换命令的使用!

From the Dr. Strangelove, or How I Learned to Love the vi Editor file: Although difficult to learn, the vi editor is well worth the effort because the skill is portable between all Unix implementations. But more importantly, vi has powerful editing capabilities that can significantly enhance your productivity. This is the first in a series of tips that will illustrate some these capabilities.

There are several formats for searching/replacing test in vi. One relatively simple format is:

"range"s/old_string/new_string/

Where "range" indicates which lines to restrict the search/replace. The "range" can be absolute or relative line numbers, as well as search criteria. A few examples will illustrate the use of this format.

vi CommandDescription
%s/red/blue/Replace first occurrence (in each line) of red with blue on all lines. The % is shorthand for all lines. Other shorthand notations include . (period) for the current line and $ for the last line in the file.
%s/red/blue/gReplace all occurrences of red with blue on all lines The g indicates all occurrences on a line. The g can be replaced with a number [1,2,...] to change only the n'th occurrence on each line.
45,54s/red/blue/Replace first occurrence of red with blue on lines 45 through 54.
.,+5s/red/blue/Replace first occurrence of red with blue starting at the current line for the next 5 lines.
/abc/,/xyz/s/red/blue/Replace first occurrence of red with blue starting at at the next line containing abc through the first line containing xyz You could also use /xyz/-4 to indicate the line before xyz.

The "range" concept applies to almost all vi commands. For example, 20,40dd will delete lines 20 through 40, .,$dd will delete the current line through the end of the file, and /abc/,/xyz/dd will delete the next line containing "abc" through the line containing "xyz."


Comment: The search/replace commands should be prefaced with the "[ESC]:" keystroke combination. It has been left out here for clarity.


转载地址:http://blog.chinaunix.net/uid-372384-id-2413870.html?page=2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值