Run Your Rspec Tests for Rails Directly From Vim
I use vim for text editing. I’m not religious about it by any means, but it is a pretty decent tool. Like most *nix tools, though, part of the art of making the most of using vim is building up a suite of your own shortcuts, commands, and re-mappings to make the editor your own and suit your own workflow.
I recently added two simple lines to my .vimrc
that have helped me practice test-driven development all the better by letting me run my tests quickly and efficiently from vim. There are entire plugins for making vim work with ruby or rails easier, but I’ve never gotten much use out of them as they represent another set of things to learn and another dependency to keep updated, and they can sometimes conflict with my existing sets of aliases and remaps.
To do this in vim, you need to edit your .vimrc
file. By default on Linux, this file is stored in your home folder. To this file, I added the following lines
1 2 |
|
What do these do? map
maps a key combination in normal mode (e.g. <leader>S
) to a command (e.g. :! rake spec <cr>
). :!
allows vim to run an external command; vim’s normal window will be hidden temporarily, the command will be run, and when it is done you can press any key to jump right back into vim. In an external command, %
will be replaced with the file path of the current buffer (file).
So, by hitting <leader>S
, I can run rake spec
and be one key press away from being back in the file I was editing1. Great! By hitting <leader>s
, I can run just the spec file that I’m currently in, saving a little time on the execution for large spec suites and keeping me from being distracted by the output of other tests.
If you use vim, I highly recommend adding a mapping like this for your own project setup. Reducing the barrier to running your tests or specs means you will run them more often, which can only help. And saving a few key strokes only makes your development experience more enjoyable.
Happy coding!
-
by default is the \ key.↩