Find and replace whole words in vim

给你一囗甜甜゛ 提交于 2019-12-18 10:02:44

问题


To find and replace all instances of a word in vim, I use

%s/word/newword/g

How do I change this so that it only finds instances of "word" that are whole words?


回答1:


You can use \< to match the beginning of a word and \> to match the end:

%s/\<word\>/newword/g



回答2:


For case-sensitive replace.. you can use "\C"

:%s/\<word\>\C/newword/g

It replaces only "word" with newword leaving others like Word,WORD... unreplaced.




回答3:


For PCRE compatible search and replace, you can use the perldo or rubydo commands as described here: http://vim.wikia.com/wiki/Perl_compatible_regular_expressions

For example:

:perldo s/\bword\b/newword/g


来源:https://stackoverflow.com/questions/1778501/find-and-replace-whole-words-in-vim

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