Today I learned

Running tests using Vim and Tmux

Screencast demo of tmux and vim

Using split panes

The most intuitive way for me to run tests is to use a tmux split pane. I simply split my screen between my editor and a shell that runs tests (npm test in this example). I've then set up a vim hotkey (,r for me) to quickly send "up enter" to the last tmux pane used.

" Repeat last command in the next tmux pane.
nnoremap <Leader>r :call <SID>TmuxRepeat()<CR>

function! s:TmuxRepeat()
  silent! exec "!tmux select-pane -l && tmux send up enter && tmux select-pane -l"
  redraw!
endfunction

Save-and-repeat

Even better, I've set up Ctrl-S as a "save-and-repeat" key to make it all happen in one keystroke:

inoremap <C-s> <Esc>:w<CR>:call <SID>TmuxRepeat()<CR>a
noremap  <C-s> :w<CR>:call <SID>TmuxRepeat()<CR>

Alternative: vim-dispatch

Alternatively, you can use vim-dispatch, but it doesn't support colors and can be disorienting.

You have just read Running tests using Vim and Tmux, written on May 24, 2015. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).

← More articles