Vim has the * (star) command which searches forward to the next (or nth, if a count is provided) occurrence of the word under the cursor. Using that command for word, one gets the \<word\> pattern in the / register (which holds the most recent search pattern).
The difference between searching for word and \<word\> is that the first pattern matches word in wordfoo, bar_word, etc., while the second does not. This is because \< and \> matches the beginning and end of a word, respectively.
I would like to know what is the best way to yank a word, similarly to how * does it (or #, its backwards counterpart), but without changing the cursor position.
If you want to put together \<, \>, and the word under the cursor to make
a search pattern in a register, why not explicitly do that?
:let @/ = '\<'..expand('<cword>')..'\>'
(Change @/ to a reference to any other register, if necessary.)
For convenience, you can make a shortcut for it:
:noremap <silent> ,/ :let @/='\<'..expand('<cword>')..'\>'<cr>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With