I'm trying to pass the word under the cursor to gtags and chain the definition search and the reference search together using a single function key.
My understanding is that this should work:
noremap <F4> :Gtags -di <cword><CR>:Gtagsa -ri <cword><CR>
But I get the following message:
Error: Line which matches to <cword> not found.
Just using <cword>
should work. And using the following from Learn Vimscript the Hard Way works fine and expands <cword>
as I would expect:
nnoremap <leader>g :grep -R <cword> .<cr>
The word under the cursor is passed to grep and searched for.
I've tried map
, nmap
, noremap
, and nnoremap
(I understand the difference, I was just trying everything). I've also tried <cword>
, '<cword>'
, and expand('<cword>')
. I've yet to find a combination that works.
The best I've been able to find is this:
nnoremap <F4> :Gtags -di <c-r><c-w><CR>:Gtagsa -ri <c-r><c-w><CR>
Which will get the word under the cursor and pass it to the definition search, but jump to the beginning of the line and pass that word to the reference search. For example, given the following with the cursor over my_func
:
void my_func(void)
The QuickFix opens with the location of the definition of my_func
, but then returns:
Error: Line which matches to void not found.
What is the proper way to map <cword>
so that both commands are run? Why does <cword>
work when passed to grep
but not Gtags
? Why does using <c-r><c-w>
jump to the beginning of the line?