Is it possible to use word under cursor in shell command? Say:
Elvis
^ cursor here
then
:r! echo <word_under_cursor>
and get "Elvis" read in.
If you are typing interactively in the vim command line you can hit CtrlrCtrlw to include the current word or CtrlrCtrla to include the current WORD.
If you are writing a function or a command you can use expand('<cword>')
or expand('<cWORD>')
for the current WORD version.
Like in:
function! Hello()
echo 'The current word is: ' . expand('<cword>')
echo 'The current WORD is: ' . expand('<cWORD>')
endfunction
<C-r>_<C-w>
solves it. Using expand() is not possible in command line mode as far as I see.
expand()
is works in a "command line" context like this: :cexpr(system("find somedir -name \\*.py | xargs grep -nsi '" . expand('<cword>') . "'"))
Commented
Aug 13 at 20:21
There is something to remember: Vim treats the commands differently. Some of them accept literals and cmdline-specials, so you can do
:r !echo <cword>
(or maybe :.!echo <cword>
)
While others accept VimScript expressions, so it could look as, for example,
:put=expand('<cword>')
(or maybe :put=system('echo ' .. expand('<cword>')
)
So the syntax of VimScript is heavily context-dependent.