4

I can search for whole words like this

/\<search term\>

\< and \> match left and right word boundaries respectively.

But how can I insert those automatically? They are awkward to type out.

5

There are two different ways to do this.

  1. You can insert those and the word currently under your cursor with *. So for example, if your cursor is on "hello", and you press asterisk, vim will treat that the same as /\<hello\><cr>. This is generally going to be the fastest way (more info). However, there's one downside. If you don't have the word you want to search for in your buffer, then you can't really navigate to that word and press *. And doing something like iHello<esc>*u isn't really that much better than just /\<Hello\>. So this is only more efficient when you are near that word. If that happens a lot, you could try method 2:

  2. Create a mapping. Since we don't want to overwrite /, this is the perfect use case for <leader>. I personally would do this:

    nnoremap <leader>/ /\<\><left<left
    

    which will insert the brackets and position your cursor between them so you can type out the word you want to search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.