Using the Gtags source code tag system with vim
The GNU Global source code tagging system is a utility that allows you to index all functions, global variable definitions, macros, etc… in a source code for easy navigation. The utility generates an index that can be used to easily locate the definition or references to a given symbol. The Gnu Global/Gtags system is very useful for hacking a large project containing many subdirectories. This article is a how-to guide for using Gtags with vim.
Obtaining and installing Gtags
The Gtags binaries are available for most linux/unix distributions. To install Gtags in ubuntu/debian, type the following in the command line:
sudo apt-get install global
Obtaining and installing the Gtags vim plugin
To integrate Gtags functions with vim, we need to install the gtags vim plugin.
- Get the plugin from this page
- Place the file gtags.vim under ~/.vim/plugin/
Customize vim macros from Gtags
After installing the plugin, it is a good idea to customize the way we use it from the vim interface for more convenience. This is an example of customized macros for Gtags (copy and paste to ~/.vimrc):
" Find definition of current symbol using Gtags map <C-?> <esc>:Gtags -r <CR> " Find references to current symbol using Gtags map <C-F> <esc>:Gtags <CR> " Go to previous file map <C-p> <esc>:bp<CR>
Final steps
After installing Gtags and integrating it with vim, we can start using it in a project. For this we need to :
- Go to the top directory of the project and type
gtags .
- Set the environment variable GTAGSLIBPATH to point to the root directory of your project. You could alternatively set it as below if your source tree is at most 3 directories deep :
export GTAGSLIBPATH=.:..:../..:../../..

Comment