mockra

Vim and Tmux Essentials - 04 May 2017


If you’ve always been curious about improving your workflow with tmux, but not sure where to start, here’s a quick start with the essentials. The first thing we’re going to start with is navigation.

A quick word of warning, these directions are for tmux 2.4, and are incompatible with older versions.

You’ll want to add these commands to your ~/.tmux.conf file. They’ll give you the ability to seamlessly switch between vim and tmux panes using a control + movement key(hjkl).

  # smart pane switching with awareness of vim splits
  bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-h) || tmux select-pane -L"
  bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-j) || tmux select-pane -D"
  bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-k) || tmux select-pane -U"
  bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-l) || tmux select-pane -R"
  bind -n C-\ run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys 'C-\\') || tmux select-pane -l"

Once you’ve setup your tmux config, you’ll also need to install a vim plugin.

  Plug 'christoomey/vim-tmux-navigator'

The next thing we’re going to do is install Vimux, which will let you easily interact with tmux from vim.

  Plug 'benmills/vimux'

Once we’ve installed vimux, we’ll want to setup our testrunner to take advantage of tmux. I personally use vim-test, which can be set to use vim with the following command in your vimrc:

  let test#strategy = "vimux"

The last thing we’ll want to do is setup a shortcut for quickly fullscreening our vimux pane. This is useful for viewing test output, or if we want to check git history, etc..

  map <Leader>z :VimuxZoomRunner<CR>

These are just the basics, and there’s a lot more you can do to boost your productivity with tmux. I think these are a good starting point for a saner test environment in vim though.

If you want to dig deeper, feel free to check out my dotfiles.