vim with ctags

December 10th, 2006 mysurface Posted in ctags, Developer, ln, vi, vim | Hits: 161370 | 26 Comments »

If you are c/c++ programmer and vim user, maybe know ctags. Most of the time, source code are distributed to many folders and each of them consist of numbers of .cc/.c and .h. Sometimes to search the definition of a function, class, enums etc by using “/” is inefficient. ctags allow fast jumping to function call even the function definition source code are from other directories.

In order to use ctags, first run the command at destination directory where the source codes are located.

ctags -R *

-R is to recursively goes across all the directories, and a ‘tags’ file will be created. You may now jump from function call to function definition, by pressing ctrl ] on the keyword.

Let say when you discover a function call which you wanna see the definition, simply point the cursor to that function and press ctrl ] and it will brings you there. If you wants to go back to where you came from, simply press ctrl t. Instead of ctrl t, I like to use ctrl i and ctrl o to travel to forward and backward of the check points.

The case is sometimes, you want to jump to the function declaration which the header files is not under the same folder of your source code. You can’t because ctags won’t know where is the function call come from.

With simple trick, you can make wonders. create a synlink under the folder of your source code to link to a specific directory or header files. Let say I wanna refers the curses.h which is located at /usr/include/, I can create a symlink in my source code folder.

ln -s /usr/include/curses.h curses.h

Check out more about symlink at here.

Then perform ctags again

ctags -R *

When you add a function at your class or module, you need to run ctags again too.

26 Responses to “vim with ctags”

  1. I am using cygwin.
    I opened cygwin bash and at the top of directory structure (windows) where the code is present, I typed “ctags -R *” and I got a segmentation fault (core dumped).

    Has anyone seen this before?

  2. SurajV: oops. how about doing ctags -R * at a folder? For examples c:/MyProgramming ?

  3. Hi, I know it’s an old post, but I’m really interested in integrating ctags in Vim. We are using Visual Studio.NET at work cauz we’re develop web sites in C#.

    I want to show to my boss that Vim can be a really relevant customized IDE :) (i’m developping on PHP at home).
    I’m using gvim on Windows XP, I downloaded the ctags .zip file from http://ctags.sourceforge.net/ and try to use the ctags.exe in it, but it doesnt work.

    Do I have to compile some file ? (there are a lot of *.c, *.h and .mak in it but i dont know what to do with them)
    Thx in advance.

    For those who are interested in developping in PHP using vim, here is a REALLY nice site which show a lot of features that turn vim into an IDE-like, similar to Zend, Ecclipse etc… -> http://www.koch.ro/blog/index.php?/archives/63-VIM-an-a-PHP-IDE.html

    Vi Rox

  4. @shine458

    I don’t have Windows machine for me to try it out. You probably need to configure the vimrc, add in this line:

    
    :set tags=tags;
    

    and run ctags.exe * -R in your folders.

  5. Am I misunderstanding something? On Ubuntu, -R is not Recursive…

    -R, –no-regex
    Don’t do any more regexp matching on the following files. May be freely intermixed
    with filenames and the –regex option.

    $ ctags –version
    ctags (GNU Emacs 22.1)

    So all I end up with is a ‘tags’ file for the current directory. The man page doesn’t even have the word ‘recursive’ in it. :-\

  6. I opened cygwin bash and at the top of directory structure (windows) where the code is present, I typed “ctags -R *” and I got a segmentation fault (core dumped).

    Has anyone seen this before?
    +=====> try with different version.

  7. @Daevid: probably your ctags aren’t exuberant enough :)

    There’s probably a package exuberant-ctags or something in the repos, or failing that, http://ctags.sourceforge.net

    I thought Ubuntu had the Right Version installed by default, but apparently not…

  8. (Incidentally, the man page still doesn’t have the word ‘recursive’, because the long form of the option is recurse. So maybe it’s called that in emacs’ ctags, and you might want to check that first.)

  9. Yes, it works under Ubuntu. When I create my tags using “ctags -R *”, I do so in the top level of my working directory. This puts the “tags” file in the top level. To overcome the problem with multiple sub-directories not finding the tags, I add the following to my ~/.vimrc file:

    :set tags=/path/to/tags

    You could do this each time you open vim, but I’m lazy. Obviously, this won’t work if you are working on multiple projects. Then you could use a relative directory instead.

  10. If ‘-R’ doesn’t work for you, then you probably have the wrong ctags program installed. Try this:
    sudo apt-get install exuberant-ctags
    sudo update-alternatives –config ctags

    …then choose ctags-exuberant. Then ‘-R’ should work! BTW, exuberant-ctags is the version that the vim team makes :~)

  11. Thanks Aaron. it worked. looks like i didn’t install the correct ctags.. i reinstalled using

    sudo apt-get install exuberant-ctags

    and it worked. it had correct definition for -R option.

  12. Thanks for this post! Just getting started using tags, seem very useful.

    I also found you can start vim and go directly to a tag:

    vi -t tag

    Or when in a file, without needing to find a reference to it:

    :ta tag

  13. Would you be all in favour of exchanging links?

  14. Thanks this was very helpful

  15. Ctags are very helpful in parsing through the code. thanks a lot again

  16. The link was so helpful while browsing the Linux Kernel source code… Thanks a lot !!

  17. Hi,what an excellent article this is,I found it on yahoo and I like it very much,I agree with what you have said, lots of things will be learned form your site,but I still have some questions with the last part,can you explain it for me ?I need your answer,and I will be back again!

  18. ravindra@msys Says:

    thanks this site is very helpful for linux kernel source code

  19. ravindra@msys Says:

    thanks a lot this link is very helpful to me thanks……………………..

  20. Thanks alot
    This link is very useful to me…………….

  21. i installed c-tags using “sudo apt-get install exuberant-ctags” command but it shows me

    usage: ctags [flags] filenames…
    -Dword Ignore “word” — handy for parameter macro name
    -F Use /regexp/ (default)
    -B Use ?regexp? instead of /regexp/
    -N Use line numbers instead of /regexp/
    -g Store static tags as though they were global (implies -h -s)
    -s Include static tags
    -e Include extern tags
    -i Include inline definitions
    -t Include typedefs
    -v Include variable declarations
    -h Add hints to help elvis distinguish between overloaded tags
    -l Add a “ln” line number hint (implies -h)
    -p Write parse info to stdout (for debugging ctags)
    -d Warn about duplicates, on stdout
    -x Write cross-reference table to stdout; skip “tags”
    -r Write a “refs” file, in addition to “tags”
    -a Append to “tags”, instead of overwriting
    If no flags are given, ctags assumes it should use -l -i -s -t -v
    Report bugs to kirkenda@cs.pdx.edu

    what to do ?

  22. Just how do you have the ability to develop a real excellent masses involving commenters to your internet site?

  23. Of course, there are two major elements that
    set Titanfall apart from other modern shooters.
    Inevitably clash of clans hack tool no survey is usually misunderstood by global
    commercial enterprises, who just can’t stand that sort of thing.

    Even while misty give, The application doesnt problem if you happen to burn ones own navy belonging to the opening in give grade
    1-3.

    My website: clash of clan hack

  24. thaks YARRRRRR thanks

  25. I have read so many content about the blogger lovers except this paragraph is really
    a nice article, keep it up.

  26. Hi colleagues, its enormous post about cultureand fully explained, keep it up all the time.

Leave a Reply