
Lately I've been doing lots of work with Ruby, and Vim has been my editor. Working with a larger codebase makes it important that your editor helps you find things easily. Here are three of the most important code navigation functions that you simply cannot do without:
- Jump to definition. In a large codebase you will find many method calls to methods you've never seen. Being able to quickly drill down through that unfamiliar code and to get back to where you started is massively important. This applies to unfamiliar classes, constants and variables as well.
- Find method usages. Often it is nice to look at how a method is used elsewhere in the code. Or if you are refactoring a method you frequently need to modify the code that uses that method.
- Plain old search. For sifting through a big codebase you have to be able to do a text search and to be able to quickly jump back and forth between possible matches.
All of this is standard in an IDE, but how do we do it in vim?
For 'Jump to definition' functionality Vim uses Ctags. What exactly is ctags? Ctags is a tool for indexing a source tree of language objects so that they can be found quickly with a text editor. Supported languages include: Assembler, AWK, ASP, BETA, Bourne/Korn/Zsh Shell, C, C++, COBOL, Eiffel, Fortran, Java, Lisp, Lua, Make, Pascal, Perl, PHP, Python, REXX, Ruby, S-Lang, Scheme, Tcl, Vim and Yacc. To install on OSX do "sudo port install ctags" on debian/ubuntu "sudo apt-get install ctags". Usage is simple. To index your code go to the root of your source tree and type "ctags -R ." (that period isn't a period it is the current directory). That will create a file called tags which is an index of your source code. Now when you open vim from that directory you can use ctags. Basic usage is to move your cursor over a method, class, or variable you want to see defined. Then type "ctrl ]" and it takes you to the definition. If you want to go back from where you have come type "ctrl r" and you return. Vim keeps a stack of places you have jumped so you can drill down deeply into the code with "ctrl ]" and then find your way back with "ctrl r". The manual is
here. A nice vim plugin to use with this is autotags. Autotags automates the maintenance of your tags files so that you don't have to keep running the ctags command when you change the code.
Try it.
Find method usages is a different problem, and unfortunately ctags won't help with it.
This website seems to have a complete discussion of possible solutions to this requirement. I played with idutils a bit, but it doesn't support ruby. In the end I gave up on a vim/ruby solution to find method usages and I fall back on general searching to fulfill this requirement. Lame-ish.
A general text search however is well supported in Vim. I like to use the
Grep plugin. It has good quality documentation so I won't bother with a lengthy description of how to install or use it. My typical usecase is to do a recursive grep across the codebase for something using the :Rgrep command. Then I can look over the various matches and the Grep plugin lets you jump around among the files. It is a very civilized way to search! I should probably point out that on my OSX setup I had to add the following lines to my vimrc
"fix grep
:let Grep_Find_Use_Xargs = 0
:let Grep_Default_Filelist = '*.rb'
Even on a codebase with a couple hundred ruby files grep seems blazingly fast so not being able to do indexed searches wouldn't be a problem for all but the largest code bases. One thing you will have to figure out once you start using the grep plugin is what vim buffers are and how they work. The Vim docs say a buffer is a file loaded into memory for editing and that all opened files are associated with a buffer. I recommend reading
this for more info. Understanding buffers will make you more efficient and change the way you work.
The common thread among all these tools and plugins is that time spent outside of your editor is time not spent coding. If you find yourself opening and closing vim every time you edit a file during a coding session, you are doing it wrong. Would you open and close eclipse every time you wanted to edit a different file? Well maybe you would if it was as fast as vim ;) But seriously. Spend some time learning how to navigate your codebase efficiently and you will become more effective.
I guess to try and put some kind of summary on this I'll just say that while it can require some effort to learn and configure, Vim can make you a very effective programmer, and with the right plugins in your arsenal I think you can outdo an IDE, even on larger codebases.