Vim

Ctags + Vim

Ctags generates an index (or tag) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object).

In easy words and as an example, if you are working on a large code base, and you want to find definition of a function foo(), with ctags installed in vim, you just need to make your cursor blink on the fucntion foo(), and use CTRL + ], and you are done, you will now be pointed to it’s definition. Ctags currently supports 41 programming languages, and it’s relatively easy to add definitions for more.

Cool right! Let’s find more about it.

This post will be about “How to use ctags in vim”.  So let’s get started:

Installing ctags


sudo apt-get install exuberant-ctags

Using ctags

If you’re currently sitting in the directory you want to index, just run:


ctags -R .

Ctags will walk through the directory recursively, tagging all source files it encounters. For very large projects, this might take a while, but normally it’s pretty fast.

You can also hide the ctag file in the .git folder, use


ctags -R -f ./.git/tags .

Using ctags from vim

If you’ve already run ctags in the current project folder, vim will automatically pick up your tags file (even in the .git directory), and you can start using it right away.

Some commands are up here.

Using Vim and Ctags with Ctrlp

Find Ctrlp plugin here.

run :CtrlPTag to search through your tags file and jump to where tags are defined.

Use this to bind this to a keyboard shortcut


nnoremap <leader>. :CtrlPTag<cr>

Ctags + Linux kernel code


make ctags #to generate ctags, now go on

Advertisements

One thought on “Ctags + Vim

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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