Vim, Rspec, and Tmux productivity booster
How a Vim function and leader lead me to the path of enlightenment
A while back I was watching a Peepcode play by play with Ben Orenstein. I gleaned a great many things from that screencast but the biggest single productivity item I picked up occurred later when observing his .vimrc file. It was a simple leader command that changed the way I test and profile code. More specifically it was 2 leader commands that called Vim functions. Since that time I have modified these Vim functions to grant more flexibility. What I am talking about? Allow me to demonstrate.
.. a simple [Vim] leader command that changed the way I test and profile code
- Probably one of the most useful aspects of a multiplexer like Tmux is having a spec run in a pane within the same window that you are editing code.
- So what are these leader commands? Let's take a look at my
~/.vimrc
. - Let's break down just one of the leader commands,
,rl
. - The other function is
RunCurrentTest
, which does the same thing as the line function except it runs all specs within the current file. - Principally, it is the fact that both Vim functions remember the last run spec command. Therefore, you can pop over to any of the files that effect the spec, make your modifications, and then type the leader command and presto the spec is again run in the right pane.
- I use the leaders that point to the
RunCurrentLineInTest
function the great majority of the time. This saves me from having to figuring out the every changing spec line number or having to mark a certain spec as "focused" with metakeys. - Secondly, although, subtle, having the save file command
:w
embedded in beginning of the leader is a big time saver when I am rapidly iterating code. - Back the argument, you can put any rspec prefixed command in the argument, such as in the figure or something like '
!ts bin/spring rspec
' - One last tip, many times I will be testing in an enviroment that I don't have formal leaders setup for. In this case I map a testing leader just for the current Vim session I am in.
Being able to have the spec results remain "statically" on the screen while you continue to make code revisions is massively superior to other approaches. Not simply for the test result output but also for receiving console-based logging feedback.
RunCurrentLineInTest
function does 2 things: (1) It automatically determines the line you are in within a spec and will perform a focussed spec e.g. bundle exec rspec spec/models/user_spec.rb:5
; (2) It saves the rspec command and if you are on a non-spec file it will run the exact same spec, "caching" it if you will.
!ts be rspec
'. The exclamation point runs a shell command within Vim. The ts
is a bash function as so:
minimul ☂ cat /Users/christian/.bash/functions function ts { args=$@ tmux send-keys -t right "$args" C-m }, which runs the spec on the right Tmux window pane. The
be
is a bash alias for bundle exec
. Last, the rspec
is well, the rspec
command.
So why is this a big deal?
Please see the screencast if this explanation is lacking.
map <Leader>zr :w<cr>:call RunCurrentTest('!ts zeus rspec')<CR>
as a Vim command.
That concludes Vim, Rspec, and Tmux productivity booster
If you want to learn more about Tmux then check out my 9-part series. In addition, although slightly dated, I highly recommend the Play by Play Ben Orenstein screencast.
If you like this article and screencast go to the dedicated page for this series at http://minimul.com/teaches/tmux.
- Pushed on 08/04/2014 by Christian