How to search for complete words in vim?

Tag: vim Author: hanshencan Date: 2009-08-07

If I search for 'string'; I want to find exactly 'string' as a word; and not 'qstring', 'sostring' etc.

Here are the options in my .vimrc

set ic
set showmatch
set smartcase
set incsearch

Best Answer

Maybe this regexp will help you: "\<string\>"

comments:

If i type in '\' to go into search mode; and then type in 's'; thenit tries to search for a 'space'; since \s means space right?
here is my sequence of keystrokes 1. \ (to go into search mode) 2. /s (this tries to find a space)
No. You need to go into search mode first and then type the pattern. So try hitting the keys exactly:- /\<string\>
@shergill: you may want to look at :h \<
If you can put your cursor on the string, then * will search forward for that exact string and # searches backwards.

Other Answer1

Use this:

\<string\>

Other Answer2

Why not just search for " string "? Works for me...

comments:

not for me; maybe i have some special flags set in vim
Doing that assumes the words don't have punctuation attached. It also won't match words that might be at the end of a line.