From charlesreid1

Vim Integration

Tmux and vim integration:

  • Set up vim keyboard shortcuts in the tmux config file
  • Add more vim-like keyboard shortcuts to navigate between panels
  • Create scripts and/or keyboard shortcuts that apply a certain session template (resizes panes, etc.)

Vim shortcuts in tmux config file

This turns on vi-like shortcuts in tmux (not really sure what this does, to be honest):

# vi is good
setw -g mode-keys vi

Add more vim-like keyboard shortcuts

First, add some keyboard shortcuts that make navigating splits and windows more like navigating splits and windows in vim:

# use vim-like keys for splits and windows
# 
#   Control-A plus:
# 
#   s = horizontal split
#   v = vertical split
#   h = pick left pane
#   j = pick down pane
#   k = pick up pane
#   l = pick right pane
# 
bind v split-window -h -c "#{pane_current_path}"
bind s split-window -v -c "#{pane_current_path}"
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind C-h select-pane -L
bind C-l select-pane -R

# smart pane switching with awareness of vim splits
# 
# Control plus:
# 
#   h = pick left pane
#   j = pick left pane
#   k = pick left pane
#   l = pick left pane
#   \ = pick another pane (?)
#
bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-h) || tmux select-pane -L"
bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-j) || tmux select-pane -D"
bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-k) || tmux select-pane -U"
bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-l) || tmux select-pane -R"
bind -n C-\ run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys 'C-\\') || tmux select-pane -l"

Now add a few more shortcuts to make life easier:

# Control-A then Control-L clears the screen
bind C-l send-keys 'C-l'

# Control-A then Control-O swaps out windows in their respective positions
bind C-o rotate-window

# Control-A plus + makes existing windows have horizontal layout
bind + select-layout main-horizontal

# Control-A plus = makes existing windows have vertical layout
bind = select-layout main-vertical

Add scripts or keyboard shortcuts for session templates

The following file should be stashed in your home dir next to your tmux config file (or anywhere else that's convenient).

This is a tmux file that defines a particular session arrangement. This session arrangement is fairly simple, it is a top-bottom horizontal split, with the bottom window taking up about 20% of the space and with the top window having the "vim" command run in it.

~/.tmux.session1

# https://stackoverflow.com/a/5753059
new-session -A -s dev -n dev
send-keys 'vim' C-m
split-window -v -p 20
select-pane -t 1

Now, you can set a tmux keyboard shortcut to source this file when the keyboard shortcut is triggered. This will run these commands and set up a new tmux session called "dev" that has the specified configuration.

To bind this to the "s" key, as in Control-A then s to apply this panel configuration, add this to ~/.tmux.conf:

# Set up easy go-to sessions with shortcuts
bind s source-file ~/.tmux.session1

My full .tmux.conf

Find my full .tmux.conf file in the following locations:

Links

Tmux cheat sheet: https://gist.github.com/afair/3489752

Why tmux: http://dominik.honnef.co/posts/2010/10/why_you_should_try_tmux_instead_of_screen/

Flags