Go back to the main page

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

  1. 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.
  2. Running a spec within a right Tmux window pane while editing code in the left.
    This section is at the 1:01 mark in the screencast.
    Minimul says —

    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.

    Here I am running a spec not to get the test results but instead for the console-based logging output via the Ruby p command.
  3. So what are these leader commands? Let's take a look at my ~/.vimrc.
  4. 1 set of commands is for Zeus and 1 set is for the regular bundle exec rspec.
  5. Let's break down just one of the leader commands, ,rl.
  6. Piece by piece explanation
    The 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.
    Let's break down the function argument, '!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.
  7. The other function is RunCurrentTest, which does the same thing as the line function except it runs all specs within the current file.
  8. The "other" leader command.
    Minimul says —

    So why is this a big deal?

    1. 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.
    2. 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.
    3. 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.

    Please see the screencast if this explanation is lacking.

  9. 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'
  10. Argument is flexible.
    This section is at the 4:31 mark in the screencast.
  11. 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.
  12. Remap a non-rspec test for current Vim session only.
    If this tip seems confusing please see around the 6th minute of the screencast.
    Just to clarify. To map a leader just for the current Vim session you would run the 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