What is the shortest keystroke to yank a c/c++ identifier in vim?
Vim has the * (star) command which searches forward to the next (or [count] if provided) occurence of the word under the cursor. Using that command for word
one gets \<word\>
at 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 doesn't. That's because \<
and \>
matches the beginning and end of a word, respectively.
I want to know what is the best way to yank a word, like * 开发者_开发知识库does (or #, the backwards version), 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 to explicitly do that?
:let @/ = '\<'.expand('<cword>').'\>'
(Change @/
to reference to any other register, if necessary.)
If it's necessary in your case, you can map this line to one- or two-key length shortcut.
:noremap <silent> ,/ :let @/ = '\<'.expand('<cword>').'\>'<cr>
I think you're on the wrong track by looking at yank options, I think you need to write a quick macro that searches for the word in the register and add the word boundary markers there. Then you can use yiw
, yaw
, yw
and all the other various standard yank commands, and your macro will deal with the adding boundaries to the search.
*
will search for the identifier, and also place the searchterm in the /
register. You can access it by pressing <Ctrl-R>/
in input-mode, or "/p
in normal mode.
This will unfortunately move the cursor to the next match. You can save the current position using marks. Save the current position with ma
, and restore it with 'a
.
The final command is ma*'a
.
The yw
command yanks from the cursor position to the end of the word. Using Vim text objects, the yaw
command yanks the whole word on which the cursor is positioned, even if the cursor is not positioned at the start of the word.
精彩评论