Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the shortest keystroke to yank a C/C++ identifier in Vim?

Tags:

vim

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.

like image 722
freitass Avatar asked Nov 20 '25 08:11

freitass


1 Answers

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>
like image 113
ib. Avatar answered Nov 22 '25 00:11

ib.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!