views:

471

answers:

2

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?

+19  A: 

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

%s/\<word\>/newword/g
sth
That works. Is that a regex syntax, or does that only work in vim?
Phenom
It is a Vim specific regexp syntax. Perl (PCRE) for example uses \b...\b instead.
Mikael S
A: 

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.

Naga Kiran