VIM & CTAGS for Erlang

I pretty much only use VI(M) for coding and CTAGS is essential to increase productivity – It makes it easier to jump through files when I need to look at a function or where some variable has been declared, amongst other things. I have been using CTAGS for years when I was coding in C, Java and most recently PHP, Python and Erlang.

Here are the steps to enable CTAGS for Erlang on Mac OS X:

1) Install the latest version of Exuberant ctags

You need to install “Exuberant Ctags” which is an enhanced CTAGS implementation of the GNU CTAGS.

Exuberant ctags make it possible to jump to the definition of a class, method, variable and any other language object (e.g. C, C++, Python, PHP, Erlang) in vim. ‘ctags’ is able to generate an index file (a.k.a tags file) for one of 41 supported programming languages. Index can be used by editors like vim to quickly find related keyword.

sudo port -v install ctags

2) Check installed version is correct (5.8)

ctags --version

 Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
 Compiled: Mar  6 2012, 23:30:52
 Addresses: <dhiebert@users.sourceforge.net>, http://ctags.sourceforge.net
 Optional compiled features: +wildcards, +regex

3) Check if the right ‘ctags’ is in the path

which tags
It should show:

/opt/local/bin/ctags

/opt is where Macports install bundles

4) Install Taglist VIM extension script:

Download taglist.zip and unzip the files to the $HOME/.vim

5) Go to the root of the project you want to generate CTAGS for

cd /projects/my_erlang_root_project_dir

6) Generate tags recursively

ctags --file-scope=no -R --languages=erlang .

7) Edit your .vimrc and add the following at the end

let Tlist_Use_Right_Window=1
let Tlist_Enable_Fold_Column=0
let Tlist_Show_One_File=1 " especially with this one
let Tlist_Compact_Format=1
let Tlist_Ctags_Cmd='/opt/local/bin/ctags'
set updatetime=1000
nmap ,t :!(cd %:p:h;ctags *)& " Maps the updates of tags to key ,t.
set tags=tags; " The ';' at the end will cause the ctags plugin to search for current dir and above dirs until it find a tag file.

" Add the following below if you want to generate ctags upon saving a file
" Auto-generate ctags upon making changes to a file
autocmd BufWritePost *.erl :silent !(cd %:p:h;ctags *)&

" If you want to auto compile (erlc) upon saving a file, then add that one as well
" Run erlc on the file being saved
autocmd BufWritePost *.erl :!erlc <afile>

8) Basic Usage

Basic usage boils down to 3 commands:

   ctrl-] takes you to the declaration of a keyword your cursor is currently over.
          Jump is made so it doesn't matter in which file it is defined. Keyword is put on the tag stack.
   ctrl-t takes you a step back in the tag stack.
   ctrl-w w toggles the keywords list window.

Additional resources:
http://ctags.sourceforge.net/faq.html#15
http://www.zalas.eu/jumping-to-a-class-function-and-variable-definitions-in-vim-with-exuberant-ctags
http://www.thegeekstuff.com/2009/04/ctags-taglist-vi-vim-editor-as-sourece-code-browser/
http://linux.byexamples.com/archives/385/vim-with-ctags-for-multi-level-directory-hierarchy/
http://ctags.sourceforge.net/

I use a gentags.sh script that I have customized a bit and put in my ~/bin – It generates tags in individual directories and I just run it initially to create all the tags, then I use ,t key in vim to update the tags for a directory where I have modified/added/removed content in a source file or have it done automatically using the ‘autocmd’ explained above. If you want the gentags.sh file, let me know and I can make it available.

Happy Erlang Tagging! 🙂

Advertisements

4 Comments to “VIM & CTAGS for Erlang”

  1. Cool! Perhaps
    let Tlist_Ctags_Cmd=’/opt/local/bin/ctags’ could be changed to
    let Tlist_Ctags_Cmd='{path/to/ctags}’

  2. Why not use Eclipse? It is the 21 century.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: