Using Emacs helm-gtags for navigating the Linux kernel sources

The Linux kernel sources are huge and without source code browsing tools like cscope or gtags, I'm lost.

After having been disappointed by the source code browsing features offered by various Emacs modes for a while, I recently discovered helm-gtags, available through MELPA.

Navigating through the Linux kernel integrates seaminglessly with my Emacs now.

This is what I have in my $HOME/emacs.d/init.el:

(add-hook 'c-mode-hook 'helm-gtags-mode)

 helm-gtags-ignore-case t
 helm-gtags-auto-update t
 helm-gtags-use-input-at-cursor t
 helm-gtags-pulse-at-cursor t
 helm-gtags-prefix-key "\C-cg"
 helm-gtags-suggested-key-mapping t

(eval-after-load "helm-gtags"
     (define-key helm-gtags-mode-map (kbd "C-c g a") 'helm-gtags-tags-in-this-function)
     (define-key helm-gtags-mode-map (kbd "C-j") 'helm-gtags-select)
     (define-key helm-gtags-mode-map (kbd "M-.") 'helm-gtags-dwim)
     (define-key helm-gtags-mode-map (kbd "M-,") 'helm-gtags-pop-stack)
     (define-key helm-gtags-mode-map (kbd "C-c <") 'helm-gtags-previous-history)
     (define-key helm-gtags-mode-map (kbd "C-c >") 'helm-gtags-next-history)))

Now, all that is required is to create a GTAGS file within your toplevel Linux kernel source directory by running gtags from GNU global. Updating the GTAGS information to reflect source changes can be done manually by running global -u. Finally, if you happen to have your Emacs open while updating GTAGS, run M-x helm-gtags-update-tags to make it reread that file.

Now, here come the most useful helm-gtags keyboard shortcuts:

With a cursor on a symbol, it jumps either the symbol definition or if you are already on a definition, it prompts you with a list of references and jumps there.
Queries you for a symbol name in a regexp fashion, prompts you with matching symbol definitions and jumps there.
C-c g r
Queries you for a symbol in a regexp fashion, prompts you with list of locations where a matching symbol is referenced and jumps there.
C-c <
After a jump, returns to the point where you jumped from.
C-c >
Undo the Undo, jumps back to the previous point you left by means of C-c <.

Author: Nicolai Stange

Created: 2018-03-11 Sun 11:26

Emacs 25.3.1 (Org mode 8.2.10)