Emacs: gtags package

This article will describe using gtags package which provides GNU Global on emacs. If you read source code with grep, you should read with gtags package because GNU Global is more effective for tracking source code.

1 Install gtags package

Build and install GNU Global.

$ wget ftp://ftp.gnu.org/pub/gnu/global/global-6.5.tar.gz
$ tar zxf global-6.5.tar.gz
$ cd global-6.5
$ ./configure --prefix=${HOME} && make all install

gtags command will be installed to ${HOME}/bin. gtags.el will be installed to ${HOME}/share/gtags/gtags.el. Please move gtags.el to your emacs's load-path.

2 ${HOME}/.emacs

Load gtags with require. Bind gtags function to global key.

(require 'gtags)
(global-set-key "\M-t" 'gtags-find-tag)
(global-set-key "\M-r" 'gtags-find-rtag)
(global-set-key "\M-s" 'gtags-find-symbol)
(global-set-key "\C-t" 'gtags-pop-stack)
gtags-find-tag Jump define of function
gtags-find-rtag Jump caller of function
gtags-find-symbol Jump user of symbols
gtags-pop-stack Return

3 Execution result

Run gtags command at the top directory. GPATH, GRTAGS and GTAGS will be generated. -v option will output tagged source code.

$ cd /path/to/src
$ gtags

Now you can track source code under the top directory.

Open source code, move cursor to main function and press M-t. This main function is set as search string of "define of function". You can modify search string manually.


List of "define of function" is displayed. You can jump to "define of function" with RET.


"define of function" is displayed.


3.1 Another GPATH, GRTAGS and GTAGS are at the under directory

If you have multiple GPATH, GRTAGS and GTAGS at the under directory, the nearest GPATH, GRTAGS and GTAGS will be used.

$ tree top
├── bar
│   └── bar.c
└── foo
    ├── GPATH
    ├── GRTAGS
    ├── GTAGS
    └── foo.c

When calling gtags function at foo/foo.c, GPATH, GRTAGS and GTAGS in foo directory will be used. When calling gtags function at bar/bar.c, GPATH, GRTAGS and GTAGS at the top directory will be used.