Search code examples
vimtaglist

taglist and local variable


When using taglist plugin in VIM we can use Ctrl + ] to jump to a global variable or a function. However if the variable is local, then it cannot find in the tags. I found from this link that we can jump to the local variables by using gd or global variable using gD.

Since this is out of scope of the taglist command, we cannot use Ctrl + t to return back to the place where we jumped from.

Is it possible to enhance taglist to jump to a local variable in case if no tag is found? And then use the stack to return back to the place where it jumped from?


Solution

  • Here's a mapping. Because the Taglist plugin actually doesn't define such (it only provides a display of tags of the current buffer, and sets up tags), no integration with it is required.

    nnoremap <silent> <C-]> :<C-u>let pos = [bufnr('')] + getpos('.')<Bar>execute 'normal!' (v:count ? v:count : '') . "\<lt>C-]>"<CR>:if pos == [bufnr('')] + getpos('.')<Bar>execute 'normal! gd'<Bar>endif<CR>
    

    This first records the current buffer and cursor position, then tries to perform the normal tag jump. If the position hasn't changed, we fall back to the gd command. Oh, actually we don't need to check the position, and can use try...catch instead. Version 2:

    nnoremap <silent> <C-]> :<C-u>try<Bar>execute 'normal!' (v:count ? v:count : '') . "\<lt>C-]>"<Bar>catch /^Vim\%((\a\+)\)\=:E426:/<Bar>execute 'normal! gd'<Bar>endtry<CR>