Configuring ctags for Python and Vim

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 πŸ™‚

14 thoughts on “Configuring ctags for Python and Vim

  1. hash1baby

    thanks for the --python-kinds=-i tip. I've been suffering silently 2 years too.

  2. Alon Horev

    You should totally try pyflakes and rope:
    pyflakes: is like lint, highlights various errors identified in static analysis (syntax errors, undefined symbols). i use it in emacs but hears it works on vim as well.
    rope: dynamic analysis for python! find-occurences, find-implementation, show-documentation, refactoring methods and much more! this too has a plugin for vim but i've never tried it.

  3. __B__

    Thanks. I've been suffering for 2 years also. A single parameter instead of more suffering. This is absolutely great.

  4. bluedome

    In step 2, there are double dashes before the options.

    Still, on OSX lion I get an error message with exuberant ctags 5.8; it says
    ctags: Unknown option: -?

  5. Oren Held Post author

    Wonil: for Linux too. It was a problem with wordpress, which turns a single hyphen into an em-dash or something. Fixed, thanks.

  6. Quiark

    I think you don't need to set output file to mytags. Just use the default and then you don't need to set anything in Vim either, it looks for tags automatically.

  7. Charlie

    Wouldn't it be better to set up a watch or a demon. Contently having ctags running in a cron job seen like it will eat a lot of cpu.

  8. Siva Kumar

    Thank you! I too had been silently suffering for many years. I'd been using cscope to navigate through the files but couldn't get ctags to work. Now, I can. Thanks once again!

  9. Mike Winter

    Thanks .ctags entries useful, but didnt need to hard-code full path to mytags, also edited .ctags to make it relative after reading ctags --help

Leave a Reply

Your email address will not be published. Required fields are marked *