Configuring ctags for Python and Vim

Source

Exuberant ctags is a cool, language-agnostic tool for creating tag files for your source code. Nice editors such as Vim, could use these tag files to implement the much needed ‘jump to definition’ feature.

Ctags is awesome, it supports Python, and is supported by Vim. It seems that the world is perfect and there’s no reason to write a post about configuring it. Well… almost.

ctags has a little downside when using Python: it recognizes import lines as a definition, at least as of ctags v5.8. No need to explain why it’s annoying in most cases. After 2 years of suffering, I’ve found it’s possible to overcome this simply by adding the –python-kinds=-i option to the command line, or better: to ~/.ctags.

And just to make it complete, a quick cookbook style for setting everything up and using:

  1. Install ctags
    e.g. aptitude install exuberant-ctags
  2. Configure ctags.
    Add to ~/.ctags the following, one option per line:

    1. –python-kinds=-i
    2. optional: –exclude=<partial names of bad files/directories>. e.g. –exclude=*/build/*to exclude all files inside ‘build/’ directories
  3. Add a cron to rebuild tags, for instance:
    1 * * * * ctags -R -o ~/mytags ~/src
  4. Configure vim:
    add to ~/.vimrc: :set tags=~/mytags
  5. Use Vim:
    1. vim -t <tag name> to open vim straight on the tag
    2. Ctrl+] to jump to tag when over a word
    3. Ctrl+T to pop back
    4. :tselect or :stselect to open
    5. :tnext, :tprev to go to next/prev tag finding
    6. :help tags for more 🙂