How to search and replace only full word in Vim

By default, the search and replace in Vim replaces the search string even if it is a substring in a longer string. For example, :%s/foo/abracadabra/ will replace foo that is in foobar[10] or zoofoomoo(20). However, while working with source code we typically want to replace only those occurrences where the search string appears as a whole word.

To do this in Vim, enclose the search string as shown in:

Tried with: Vim 7.4 and Ubuntu 14.04


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.