Tuesday, 12 November 2013

Using Ctags to navigate software projects in Vim

Most IDEs and text editors have a "jump to definition" feature that allow you to easily navigate source code; you can achieve the same in Vim by using Exuberant Ctags:
  • Most Linux distributions will have it in their package manager; e.g., in Ubuntu:
    sudo apt-get install exuberant-ctags
  • Go to your project directory and run: 
    ctags -R [-o outputfile]

          Note: you can add a cron job to do this periodically (in order to account for source code changes).
  • Add the following to ~/.vimrc:
    set tags=tags  " (or whatever you used for outputfile)

    Note: each project will have its own tags file; vim will look for it in the folder it was launched from (so you will probably want to run it from the same location as ctags -R).
  • Optional: exclude the tags file from version control (e.g., .gitignore)
To navigate to a definition in Vim, navigate to the symbol and press Ctrl+]; to go back use Ctrl+T. For help use :help tags.