sponsor Vim development Vim logo Vim Book Ad

basic Tip #1455: Jumps to a local/global definition by same key

 tip karma   Rating 4/4, Viewed by 1009 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   January 4, 2007 2:58      complexity:   basic
author:   Shotaro Aoyama      as of Vim:  

When you want to jump to a definition of a variable,
what do you do?
Use C-] or gd?
C-] finds only global variables (and functions. Ctags extracts
only global objects).
On the other hand, gd detects only local variables.
I think it's a bit complicated to choice them by a situation.
So I wrote this function:

function! GoDefinition()
  let pos = getpos(".")
  normal! gd
  if getpos(".") == pos
    exe "tag " . expand("<cword>")
  endif
endfunction
nnoremap <C-]> :call GoDefinition()<CR>

This function first does gd to try to find a local definition
of a variable under the cursor, and if it failed then probably
the variable is a global variable, try :tag.

This way, you can jump to the definition of both local and
global variable by same key sequence.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Shows what function the cursor is in. (for C/C++) | A set of two commands and one function to provide user-preferred options setting >>

Additional Notes

Anonymous, January 4, 2007 9:45
r6u6
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo