👇👇👇tl;dr # .vimrc set tags=tags autocmd BufWritePost *.py silent! !ctags -R --python-kinds=-i --languages=python 2> /dev/null & $ brew install ctags
In the last two years I've spent a significant amount of time in PyCharm and a somewhat shorter time with Visual Studio Code, both with vim keyboard bindings, writing mostly Python and JS. Ultimately, I came back to some flavor of terminal-based vim: First Vim 8, now Neovim. There were too many missing features in "vim mode" in both of the IDEs, and the speed difference—brain-to-screen—was noticeable compared to vim.
What I Miss From IDEs
Because I'm not yet a
grep expert in the context of search and replace, I still miss the
refactoring tools in PyCharm for renaming and reorganizing code. Automatic imports is also
something I haven't replicated in vim.
However, what I was missing most was "go to definition", which I had mapped in VSC and PyCharm to
ctrl-] as it is by default in vim.
Getting It To Work + Timesucks to Avoid
As with most of my efforts to make vim more ergonomic/IDE-like, getting "go to definition" working took longer than I had hoped.
For the uninitiated, while vim comes with ctrl-] out of the box, it
doesn't actually know where something is defined unless there's at least one
tags file and you've told vim
where to find it/them. You can run
ctags manually, but this can get tiresome if you want vim to
always have an updated idea of where all the definitions are in your project: functions, modules,
constants, classes, types, etc.
The first option I found for automatically updating the tags file didn't work as advertised: I tried to set up git hooks like Mr. Pope suggested, but for whatever reason the tags file never refreshed on commit. Avoid this rabbit hole! And anyway, don't you want "go to definition" to work between commits too?
What ended up working to refresh the tags file *on every save* was a modified version of something I found in the comments of a StackOverflow answer.
python-kinds=-i, "go to definition"
didn't work as expected on MacOS (it was fine on an Ubuntu droplet). Inspecting my
it was including imports, which caused my
ctrl-] invocations to only jump to the top of the current
module, where the import was, not to the definition of the entity.
Final Product + Developer Experience
I can now very quickly navigate to the definition of whatever's under my cursor with a single keystroke. What's nice about this usage of ctags is 1) it runs in the background and never interrupts you, 2) it's fast, and 3) it runs every time you save. I haven't tried this at all with JS yet (will be trying this), but it is lightning fast and accurate for jumping around in large Python-based projects.
gf bridges a significant gap that ctags don't cover:
It stands for "go to file". Type
gf in normal mode when your cursor's over a filename, and it opens it!
Next Thing to Try: Tagging dependencies and the Python standard lib
As alluded to above, you can tell vim about multiple
tags files in multiple paths. It
doesn't come up as often that I want to "go to definition" of library code, but I can see how it
might come in handy.