Tmux for Vim users

Tmux is a terminal multiplexer with a more elegant configuration and more powerful feature set than GNU Screen. Tmuxinator is a layout manager for tmux which makes managing complex tmux sessions easy. If you work on several projects at once like I do, Tmuxinator allows you to run several tmux session configurations at once, meaning switching contexts is as easy as typing a shell command. Tmux also allows full customization of its keyboard shortcuts, meaning if you're accustomed to Vim's keybindings, you can enable yourself to feel right at home. There are plenty of tmux tutorials online, so check those out for details about setting up your environment. Instead, I'd like to go over the contents of my `.tmux.conf` file so you're able to get comfortable in tmux quickly as a seasoned Vim user.

First, I set the tmux prefix key combination to Ctrl+A instead of the default Ctrl+B. This means that the prefix key combination can be pressed using only your left hand. And, if you remap your Caps Lock key to trigger Ctrl instead, both keys are right next to one another, making it easy to switch panes, create new ones, etc.

set -g prefix C-a

Next, I set the history limit to 100000 lines. This allows scrolling back as far as you'll ever need using Ctrl+A [.

set-option -g history-limit 100000

There are a couple settings that make Vim itself more pleasant to use inside of tmux. To ensure keyboard shortcuts inside Vim still work, we need to enable XTerm keybindings. And to be sure Vim's colors aren't distorted, we enable 256 color mode:

setw -g xterm-keys on
set-option -g default-terminal "screen-256color"

The default keybindings for splitting windows are poorly defined in the % key. To provide more memorable shortcuts, I've bound them to | and - for vertical and horizontal splits, respectively. This means you can press Ctrl+A | to split your current pane into two vertically, and Ctrl+A - to split it horizontally.

bind-key | split-window -h
bind-key - split-window

Next, to match Vim's / search, I enable the vi key mode.

setw -g mode-keys vi # I especially like being able to search with /,? when in copy-mode

One of my most commonly used Vim features is the Ctrl+W pane navigation commands. These allow easy navigation between all your visible editor panes. This behavior can be mimicked in tmux by binding the hjkl keys to the select-pane command:

unbind-key j
bind-key j select-pane -D

unbind-key k
bind-key k select-pane -U

unbind-key h
bind-key h select-pane -L

unbind-key l
bind-key l select-pane -R

Vim and Tmux are elegant tools on their own, but a veritable developer utopia when used together. I love my new terminal environment and can't wait to put it to work for me. Cheers!