I want to have a function that vimgrep for the word under the cursor appended to a predefined string.

I have defined this command:

command! -nargs=0 GJL vimgrep <cword> *.tex | copen

which does the job for only the current word. Now I want to grep for ref{ + <cword>. That is, in the case that <cword> is eqn1, I want to grep for ref{eqn1.

Any idea how to set up this function in a .vimrc?


Use exec with string concatenation:

command! -nargs=0 GJL exec 'vimgrep ref{'.expand('<cword>').' *.tex | copen'

In a function, you'd do something similar, but more neatly, I suppose:

function Foo()
    let l:pattern = 'ref{' . expand('<cword>')
    exec 'vimgrep' l:pattern '*.tex | copen'
  • Works like a charm. – Jan Mar 20 '17 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.