Improve Your Ruby Workflow by Integrating vim/tmux/pry

Motivation

When I joined Crowdtap last June, I needed to massively refactor one of our applications, and I needed to seriously speed up my workflow.

I wanted things to be instant and interactive. I needed to feel closer to the code.

I tried Spork, but found it limited in what it could do: switching from rspec/cucumber is hard, reloading is problematic. Zeus lifts these limitations, but introduces new ones. Regardless, These solutions solve the instant problem to some extent, but do not address the interactive one.

irb-config

I am approaching the problem differently. Instead of having to use a preloader, I found that staying in the ruby interactive shell turns out to be very effective. To support my workflow, I made irb-config. It allows running RSpec/Cucumber tests directly in the rails console.

irb-config sits on top of RVM, bypasses Bundler to load Pry, Pry Doc, Pry Rails, Pry Debugger, Pry Stack Explorer, Awesome Print, a Mongoid pretty printer with Coderay, and a Gnuplot integration.

Workflow

I am a heavy user of Vim, but it doesn’t have a shell like Emacs. Fortunately, we have tmux to the rescue. By using the vim screen plugin, we can integrate vim and irb-config, which is more powerful than using something like guard-rspec in my opinion.

Let me share my workflow with you.

download mp4 (09:13 / 720p / 26Mb)

Want it?

1. Install irb-config

git clone git://github.com/nviennot/irb-config.git ~/.irb
cd ~/.irb
make install

You don’t need to change any of your projects. irb-config is pervasive:
When the .irbrc file is loaded, irb-config adds the global gemset to Bundler, and loads Pry.

2. Setup some vim bindings

a. You can use my vim-config:

git clone git://github.com/nviennot/vim-config.git ~/.vim
cd ~/.vim
make install

b. Or install the screen plugin and add these bindings to your config:

" Screen settings
let g:ScreenImpl = 'Tmux'
let g:ScreenShellTmuxInitArgs = '-2'
let g:ScreenShellInitialFocus = 'shell'
let g:ScreenShellQuitOnVimExit = 0
map <F5> :ScreenShellVertical<CR>
command -nargs=? -complete=shellcmd W  :w | :call ScreenShellSend("load '".@%."';")
map <Leader>r :w<CR> :call ScreenShellSend("rspec ".@% . ':' . line('.'))<CR>
map <Leader>e :w<CR> :call ScreenShellSend("cucumber --format=pretty ".@% . ':' . line('.'))<CR>
map <Leader>b :w<CR> :call ScreenShellSend("break ".@% . ':' . line('.'))<CR>

3. Setup a proper tmux config (optional)

git clone git://github.com/nviennot/tmux-config.git ~/.tmux
cd ~/.tmux
make install

If you are using OSX you will have to fiddle your Terminal settings to enable the Alt key.
If you are using Linux, you are good to go.
If you are using Windows, you deserve better.

Notes

Starting in the dev environment

When doing testing in the console with irb-config, always run rails c and let irb-config switch between your development environment and test environment. This will allow painless reloading.

If you application has some gems that do not like to have their environment switched, start the console with rails c test. But be careful, you will have to use :W to reload the files you are changing.

Config files references


Published: November 19 2012

Edited: December 08 2012

blog comments powered by Disqus