Use RSpec.vim with tmux and Dispatch

(And Keep Your Screen Real Estate)

This post originally published on thoughtbot's Giant Robots blog.

As you may know, there are quite a few of us now working at thoughtbot. While we share a lot of the same opinions, we all work a little differently. Most of us are running specs from Vim using RSpec.vim. Some of us use RSpec.vim with tslime. I enjoy a different setup with Dispatch.


I like to use my whole screen for editing or referencing documentation in a browser, so I don't keep multiple tmux splits most of the time. Heck, I don't even have tmux open half the time.

Instead of using tslime.vim to send tests outside of Vim, I enjoy using Tim Pope’s Dispatch to automatically open a new tmux split to run asynchronously, or to send the command to Bash to be run synchronously. Either way, the output of the command is captured and parsed if it exits with a non-zero status. This allows for known commands, such as rspec, to give a one-line test failure message and a link to the failing spec.

This output is captured inside a Vim split which you can easily move to and use ^] (Ctrl-]) to have Vim jump directly to the failing test. This is extremely useful when there are failures in files you don't already have open.

Here's a video showing what Dispatch is all about:

Setting things up

While either Dispatch or RSpec.vim could be used independently, they have a real power when used together. Dispatch gives you the power of running the test commands from RSpec.vim asynchronously and captures the output for use in Vim.

Here are the changes I've made to my .vimrc:

Bundle 'thoughtbot/vim-rspec'
Bundle 'tpope/vim-dispatch'

let g:rspec_command = "Dispatch rspec {spec}"

One minor downside to using Dispatch is that you don't see the backtrace in the failure message, so if you need to see that to debug I recommend running the spec outside of Vim. I recommend using ^Z (Ctrl+Z) to send Vim to the background, running the specs, and using fg to bring Vim back to the foreground.

Regardless of the implementation detail of how you run your specs, we think that RSpec.vim is a great way to tighten the code/test feedback loop. Impress your friends with how great you're doing!

Written by Caleb Thompson