Find and replace whole words in vim

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

%s/word/newword/g 

How can I change this so that it finds only instances of the β€œword,” which are whole words?

+41
vim editor regex replace
Nov 22 '09 at 11:51
source share
3 answers

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

 %s/\<word\>/newword/g 
+78
Nov 22 '09 at 11:53
source share

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

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

It replaces only the "word" with a new word, leaving others like Word, WORD ... unreplaced.

+10
Nov 24 '09 at 20:25
source share

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

For example:

 :perldo s/\bword\b/newword/g 
0
Sep 07 '16 at 23:39
source share



All Articles