TestNav: Easier test file navigation in Vim
Automatically navigate from a production code file to its corresponding test code file in Vim
This past year, I switched my everyday text editor from Sublime Text to Vim. One of the key features that I missed from Sublime Text was the ability to quickly and automatically navigate from a production code file to its corresponding test code file and back again. In Sublime Text, this functionality was provided by a plugin called RubyTest.
Upon switching to Vim, I immediately started looking for a replacement for toggling between production and test code. I started out using Gary Bernhardt’s excellent .vimrc which includes a script that functions very similarly to RubyTest. However, the script is Rails specific and didn’t work for any other language besides Ruby.
These limitations inspired me to build my first plugin, TestNav. Similar to the plugin and script that inspired TestNav’s creation, the plugin by default binds
<Leader>. to a function that will do a best guess to navigate you from the current file to the corresponding test or production file.
Currently, the plugin only works assuming you follow a few conventions:
- You launch Vim in the root directory of your project (or the Vim working directory is set to the root directory of your project).
- Your test file names end with
_specand the corresponding production file name matches without the ending. For example,
user_spec.rbcontains the specs for the logic defined in
- Your Vim is compiled with ruby support and the Unix
findcommand is available on your system.
To install the plugin, I recommend using pathogen. If you have pathogen installed, all you need to do is execute the following command to get the latest version of TestNav:
cd ~/.vim/bundle && git clone email@example.com:davebrace/vim-testnav.git