vim中的替换操作

  在vim中 :s(substitute)命令用于查找并替换字符串。使用方法如下:

:s/<find-this>/<replace-with-this>/<flags>

  例如:

1 :%s/foo/bar/g  # 在全局范围内(%)查找foo并将之替换为bar,所有出现都会被替换(g)
2 :s/foo/bar/g  # 在当前行内查找foo并将之替换为bar,所有出现都会被替换(g)
3 :'<,'>s/foo/bar/g  # 在选区内进行替换,Visual模式下选择区域后输入会自动补全'<,'>

  下面是一些可以加的flag.    for example,  :s/cat/dog/gi  会把cat.Cat() 变成 dog.dog().

  g —global replace: replace every occurrence of the pattern, not just the first one

  c —confirm each substitution: prompt the user before replacing the text

  e —do not show errors if no matches are found

  i —ignore case: make the search case-insensitive

  I —make the search case-sensitive

  最后说一个小tip: 程序员喜欢用foo和bar指代变量名: foo bar -> Fucked Up Beyond All Repair 完全无法修补。

猜你喜欢

转载自www.cnblogs.com/chester-cs/p/11605656.html