在vim中查找并替换整个单词。

时间:2022-09-13 09:48:36

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

为了在vim中查找和替换一个单词的所有实例,我使用

%s/word/newword/g

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

我如何改变它,使它只发现“单词”的实例,这是完整的单词?

3 个解决方案

#1


86  

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

你可以使用\ <来匹配一个单词的开头和\> 来匹配结尾:

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

#2


11  

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

为区分大小写的取代. .您可以使用“\ C”

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

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

它将“word”替换为“newword”,而将“word”替换为“word”。未替换。

#3


0  

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

对于PCRE兼容的搜索和替换,您可以使用perldo或rubydo命令,如下所示:http://vim.wikia.com/wiki/Perl_compatible_regular_expressions

For example:

例如:

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

#1


86  

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

你可以使用\ <来匹配一个单词的开头和\> 来匹配结尾:

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

#2


11  

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

为区分大小写的取代. .您可以使用“\ C”

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

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

它将“word”替换为“newword”,而将“word”替换为“word”。未替换。

#3


0  

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

对于PCRE兼容的搜索和替换,您可以使用perldo或rubydo命令,如下所示:http://vim.wikia.com/wiki/Perl_compatible_regular_expressions

For example:

例如:

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