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:
- Install ctags
e.g. aptitude install exuberant-ctags - Configure ctags.
Add to ~/.ctags the following, one option per line:- --python-kinds=-i
- optional: --exclude=<partial names of bad files/directories>. e.g. --exclude=*/build/* to exclude all files inside 'build/' directories
- Add a cron to rebuild tags, for instance:
1 * * * * ctags -R -o ~/mytags ~/src - Configure vim:
add to ~/.vimrc: :set tags=~/mytags - Use Vim:
- vim -t <tag name> to open vim straight on the tag
- Ctrl+] to jump to tag when over a word
- Ctrl+T to pop back
- :tselect or :stselect to open
- :tnext, :tprev to go to next/prev tag finding
- :help tags for more π
But the _real_ fun begins when you use cscope (http://cscope.sourceforge.net/) (support is by default built-into vim) and pycscope (http://pypi.python.org/pypi/pycscope/0.3) to generate the tags.
Sounds awesome, gotta try it π
10x
thanks for the --python-kinds=-i tip. I've been suffering silently 2 years too.
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.
Thanks. I've been suffering for 2 years also. A single parameter instead of more suffering. This is absolutely great.
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: -?
Should use '--python-kinds=-i' for Mac OS X
Wonil: for Linux too. It was a problem with wordpress, which turns a single hyphen into an em-dash or something. Fixed, thanks.
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.
Quiark: interesting, simplifies things, thanks! π
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.
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!
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
The cron is sub-optimal. Try updating the tags file on buffer write.
https://github.com/craigemery/vim-autotag