Are you a C programmer?? and do you use vi ?? Well then.This post may supplement you for enhancing your editor configuration so as to have an easy coding environment.

Typically, a fairly good project will have multiple source files and header files organized into many folders like bin, src, header,obj etc. Its often the case that you may define a function anywhere and you may refer it at someother location. If you are a person like me, then surely you will forget the the things like place of the implementation and the things like parameters, return type etc. In those situations you have ctag to help you out.

Supposing that you have a folder called ‘project’ and within it you have multiple folders, .c files and .h files.

Now change the dir to “project” and type

$ctag -f myproject_tag -R

This will create a tag file called myproject_tag.This file will contain the information about your protoypes and implementation of your function, info about global variables, macros etc. The -R indicates that it is ‘recursive’, so that, the tag file will include all info about all .c and .h files under the ‘project’ folder hierarchy.

Now we have created a tagfile.We have to integrate it with vi so that the vi can get to know those information.

Now change to home directory and open the hidden file .vimrc (If its not there create it) and set the path to the tagfile which vi can search for.So now type,

set tags=/path-to-project-folder/myproject_tag

So now.. its set.Now go to one of your C file using vi and keep your cursor on a function call (whose implementation is in someother file) and press “ctrl+]”.It will take you to the implementation file.To get back to the original caller line press “ctrl+t”.

You can set paths to multiple tag files in .vimrc, by separating those paths with a ‘comma’. Suppose you have two projects “prj1″ and “prj2″ and you would like to have tag files separately for each project.Then in that case a comma separated path can be used.Also make sure that you give absolute paths.

Also, dont forget to run ctag again, when you have added a new thing (like function/global variable/whatever ).In that case you don’t have to edit your .vimrc.

Also know that this ctag has many variants, which can be used for many other programming languages and along with many other editors.

So..With this extra bit of info have a happy coding environment.(Sounds like an ad huh :-) )

blog comments powered by Disqus


21 July 2010

Fork me on GitHub