Wednesday 23 February 2011

gvim, ctags, and linux kernel

gvim (sudo apt-get install vim-gnome or yum install gvim) is a powerful editor for handling C code, specially for kernel hacking. Keyboard shortcuts I use frequently with gvim (all keys are for ro mode and typed on gvim console by hitting esc + ":"): 
  • Opening another file: e file_path_name  (shows possible file names with a tab)
  • Jumping to a definition: tag tag_name (supports auto complete with a tab)
  • Searching: /string (for instant search) or :/string. To move around with the selection use 'n' or 'shift + n' (for a reverse search). 
  • Cut, copy and paste: Use 'dd' for cut, 'yy' for copy, and 'p' for paste. For multiple lines enter the number like 100 and then press 'dd' or 'yy' to cut or copy 100 lines. 'p' will paste 100 line at current cursor location. Press 'u' for undo. They also work in visual mode and partial line selection with 'v' and arrow keys. 
  • Jumping around in a file: To jump to a particular line number, enter line number at gvim console. For example :1024. To jump at the end of a file use 'shift + g' or 'G'.  
  • Tagged jumps: put cursor under C symbol e.g. do_softirq and then press 'ctrl + ]'. gvim has notion of tag stack, so the order of tag jumps are saved. To pop up or jump out use 'ctrl + t'.
All these tricks are equally valid for vim. But I find it more easy to navigate in gvim. 

Ctag (http://ctags.sourceforge.net/) and gvim make life much more eaiser when managing something big like Linux kernel. Linux kernel has multiple definitions for a symbol (see kmalloc). On some distributions, by default, gvim jumps on to the first tag instead of showing a jump selection list. During or after the jump, it will show at bottom something like this "1 out of 4". I always prefer to see a list of possible jump targets when there are more than one definitions are present. To generate the list of tags (if there are multiple selections present) use 'g' then 'ctrl + ]'. An additional key press ...not good !  Also I don't wanna get bothered every time for pressing 1 when there is only one jump target. 

So after going through some gvim manual reading I figured out the way to do it. Simple trick is to map key combination of "ctrl + ]" to "g + ctrl + ]". Put something like this in your .gvimrc or .vimrc file

set tags=tags;
nmap <C-]> g<C-]>
vmap <C-]> g<C-]>

And thats all. Also instead of generating tags by using something like 


$ ctags -R *


let kernel make file generate tags for you. Use 

$ make tags

It will eliminate a lot of dead code from arch specific sections.  

No comments:

Post a Comment