Vim Tips Wiki
Register
Advertisement

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 8 Printable Monobook Previous Next

created 2001 · complexity basic · author Yegappan · version 6.0


To jump to the declaration of a local variable in a C program, position the cursor on the name of the variable and use the gd command in the normal mode.

To jump to the declaration of a global variable in a C program, position the cursor on the name of the variable and use the gD command in the normal mode.

Comments[]

This works in Java, Perl and a number of other languages as well. However sometimes gD does not find the definition of a function but a mere call to it. A function below is a simple search for the function at cursor definition (C, C++).

function! GotoDefinition()
  let n = search("\\<".expand("<cword>")."\\>[^(]*([^)]*)\\s*\\n*\\s*{")
endfunction
map <F4> :call GotoDefinition()<CR>
imap <F4> <c-o>:call GotoDefinition()<CR>

To jump back use `` (backquote backquote).

You can also jump back using Ctrl-o or '' (single quote, single quote).


ctags can be used to do a more comprehensive job. Make 'tags' file with: ctags -R *.cpp *.h

Then, goto any variable and pressing Ctrl-] will take you to the declaration/definition. Same for functions as well. Use Ctrl-t to return back.


You can also use cscope for this. http://cscope.sourceforge.net/ http://cscope.sourceforge.net/cscope_vim_tutorial.html


Advertisement