Fandom

Vim Tips Wiki

Jumping to the declaration of a local/global variable

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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.

CommentsEdit

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 qoute, 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


Also on Fandom

Random Wiki