How to generate a ctags files and use it with vim

The ctags command allows you to index source – any source. The command generates a single file called a tags file (which [ironically] is the name of the file). Then you can point to this file with editors like VIM/GVIM/EMACS, etc. for auto-complete and for cross-probing.

Let’s see it in action…

First tag the source you want to index using:

ctags -V -R --c++-kinds=+p --fields=+iaS --extra=+q --languages=c++ .

This will recursively index all files from this point down and generate a ‘tags’ file in the current directory. You should open the tags file and look around to see if it’s getting what you expect and that it isn’t getting things that you don’t expect.

From here we can open vim and start auto-completing. This works because VIM will automatically search the current directory for a tags file, which we just generated. If you want to point to a tags file that lives in another directory, then use:

:set ctags=/path/to/file/with/tags

Now you can start typing. Let’s act like we have a class called MyClass, in VIM you must be in insert mode (press ‘i’) then type…


…and a drop-down menu should show with the option to auto-complete to MyClass (presuming some MyClass.cpp was indexed with the ctags command).

CTRL+N : to go forward through tags

CTRL+P : to go backward through tags

Now the cool part. Once you’ve auto-completed a word or you put your cursor on any keyword and press CTRL+], then you will see a menu that will let you pick the implementation you would like to go to and once selected VIM will open the source file that defines that keyword.

CTRL+] : to push into tags

CTRL+T : to pop out of keywords



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s