I know that to search for a whole word in vim you need to type:
/\<word\><CR>
Now, what I would like to do is to map this behaviour to ? (as I never search backwards, and if needed I could search forward and then NN). I.e. I’d like to type:
?word<CR>
and have the same result as above (vim searches the whole word). I’ve been fiddling around with vim commands and mappings for some weeks now, but I’m not sure about how to accomplish this one. Thank you for any help.
Update: (insead of ? I use \ now).
I tend to use
*and#(as suggested by Brian Agnew), but if you want a method that involves typingyou could do something like this:
Note there is a space after
SearchWordon the last line.Explanation:
The mapping will make
?open up a command prompt and typeSearchWord(including the space). The command makesSearchWord myworddo the equivalent ofcall SearchWord('myword')(i.e. it puts the quotes round the argument in order to make it into a string). The function sets the search register@/equal to your word surrounded by\<and\>and then does a normal-modento find the next instance of the contents of the search register.Of course you lose the benefits of incremental searching if you do this, but hopefully it’s useful anyway.