Search for the whole word Vim - but faster

I know that to search for a whole word in vim you need to enter:

/\<word\><CR> 

Now, what I would like to do is map this behavior? (since I never looked back, and if necessary I could search forward, and then NN). That is, I would like to type:

 ?word<CR> 

and have the same result as above (vim searches for the whole word). I have been doing virtual teams and comparisons for several weeks now, but I'm not sure how to do this. Thanks for any help.

Update: (insead of? I use \ now).

+4
source share
3 answers

I usually use * and # (as suggested by Brian Agnew), but if you want to use a method that includes input

 ?word<CR> 

you can do something like this:

 function! SearchWord(word) let @/ = '\<' . a:word . '\>' normal n endfunction command! -nargs=1 SearchWord call SearchWord(<f-args>) nmap ? :SearchWord 

Notice that the last line has a space after SearchWord .

Explanation:

Does the mapping do ? open a command prompt and type SearchWord (including space). The command makes SearchWord myword equivalent to call SearchWord('myword') (i.e., puts quotation marks around the argument to turn it into a string). The function sets the search register @/ to your word surrounded by \< and \> , and then executes normal mode n to find the next instance of the contents of the search register.

Of course, you lose the benefits of incremental search if you do this, but hopefully it is still useful.

+11
source

You can search for words under the cursor using * and # (forward and backward). g* and g# will do the same, but find words containing what is under your cursor initially.

Obviously (!) You need to find the first instance for this to work, but it is very effective for sequential searches.

+8
source

Al solution is very nice, but isn’t it easier to just enter the template \<\> and then move the cursor twice? This works for me:

 nmap ? /\<\><Left><Left> 

That way you still get incremental search (kinda).

+4
source

Source: https://habr.com/ru/post/1336595/


All Articles