vim中的替换操作

好久不见. 提交于 2019-11-30 17:07:22

  在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 完全无法修补。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!