My Wiki!

Tutorial: browsing source code with vim

How to read source code with vim:

  • open main.c
  • see a function?
    • place cursor at the beginning of function name and '#' to search for function definition.
    • if function is defined somewhere else:
      • open main.c in another window: ':vsplit main.c'
      • change to the new window and 'n' or 'N' to search.
      • '!grep -ri functionname *'. * Tip: to avoid typing the functionname, you can double click on the function name in the previous step then use 'shift + ins'.
        • Tip: to avoid mouse click use 'ctrl + R /' to get the last search term :)).
      • open file.c that seems to contain the definition of this function according to output of grep.
      • 'n' or 'N' to search inside this file.

ctags

Install exuberant ctags

yum install ctags-etags.x86_64

Configure .vimrc

  # search current dir.
  set tags=./tags;    
  let Tlist_Ctags_Cmd='/usr/bin/ctags'
  
On the Wiki
    Wiki Activity
    Random page
    Videos
    Photos
    Chat
Community portal
To do

Contribute Share Watchlist Random page Recent changes Single tags file for a source tree Edit Talk0 1,599pages on this wiki Tip 804 Printable Monobook Previous Next

created 2004 · complexity intermediate · author Dubhead · version 6.0

At the top of a source tree, create a tags file by

% ctags -R

This assumes Exuberant Ctags. The -R (or –recurse) option tells ctags to recurse into directories.

Then, set the tags option in vimrc as:

 set tags=tags;

The last semicolon is the key here. When Vim tries to locate the 'tags' file, it first looks at the current directory, then the parent directory, then the parent of the parent, and so on. This setting works nicely with 'set autochdir', because then Vim's current directory is the same as the directory of the file. If you do not like using 'autochdir' but want the same upward search, use:

 set tags=./tags;

Here, the leading “./” tells Vim to use the directory of the current file rather than Vim's working directory.

Create tag file

ctags -R

ctags -n -f [OUTPUT] [SOURCE] to generate the tags (NOTE: the -n applies to me but may not be necessary for your usage)
exec "set tags=" . [OUTPUT] inside of .vimrc to let vim become of aware of the tags

http://vim.wikia.com/wiki/Browsing_programs_with_tags


Navigation