Wikia

Vim Tips Wiki

Changes: Jumping to the declaration of a local/global variable

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 33: Line 33:
   
 
----
 
----
ctags can be used to do a more comprehensive job. Make 'tags' file with: {{tt|ctags -R *.cpp *.h}}
+
ctags can be used to do a more comprehensive job. Make 'tags' file with: <code>ctags -R *.cpp *.h</code>
   
 
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.
 
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.

Latest revision as of 12:27, July 15, 2012

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


Around Wikia's network

Random Wiki