Vim in Tmux configuration

About the configuration

Over the years, I’ve modified my tmux configuration countless times. While I’ve experimented with alternatives like Zellij and Screen, I always come back to tmux due to its superior customization options that allow me to tailor it to my needs. Of course, there are always challenges to overcome in order to get everything working perfectly, and that’s exactly what I’ve done with this setup.

My goal for this configuration is to keep it both simple and effective. Since I do a lot of copy-pasting in my terminal, having Vim keybindings and seamless mouse movement is crucial, without interruptions. One issue I frequently encounter is tmux resetting the screen during copying, preventing me from redoing a copy if something goes wrong.

This configuration resolves all the issues I’ve mentioned, and I’ll also share a helpful tmux logging trick to easily retrieve your I/O (input and output) whenever needed.


My configuration

This configuration provides the following features:

  • Vim motions
  • Vim-style copy-pasting, navigation, and selection
  • A high history limit (for handling large outputs)
  • Copying to the system clipboard (using xclip)
  • Rebinding Ctrl + b to Ctrl + x

Please note that this setup uses the tmux plugin manager (TPM). To install it, run the following command:

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

The .tmux.conf file

bind r source-file ~/.tmux.conf

# Changing CTRL + B to CTRL + x, since ssh sessions
unbind-key C-b
set-option -g prefix C-x
bind-key C-x send-prefix

# Switch panes using ALT HJKL
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

set-option -ga terminal-overrides ",xterm-256color:Tc"

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
set -g @plugin 'christoomey/vim-tmux-navigator'

# Quality of life stuff
set -g history-limit 10000
set -s escape-time 0
set -g allow-rename off

# Join Windows
bind-key j command-prompt -p "join pane from:"
bind-key s command-prompt -p "send pane to:"

# Search Mode VI (default is emacs)
set-window-option -g mode-keys vi
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -selection clipboard -i'
bind P paste-buffer
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -selection clipboard -i'

bind -n M-h select-pane -L
bind -n M-j select-pane -D
bind -n M-k select-pane -U
bind -n M-l select-pane -R



# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "send-keys C-l"

Logging your input and output from tmux

Two lines to add to your configuration will log all terminal output, per pane and per window, to a specific directory located at .tmux.log:

set-hook -g session-created 'run-shell "mkdir -p ~/.tmux.log && tmux pipe-pane -o -t #{session_name}:#{window_index}.#{pane_index} \"exec cat >> ~/.tmux.log/#{session_name}_#{window_name}_#{window_index}_#{pane_index}_$(date +%Y-%m-%d-%H%M%S).log\""'
set-hook -g window-pane-changed 'run-shell "tmux pipe-pane -o -t #{session_name}:#{window_index}.#{pane_index} \"exec cat >> ~/.tmux.log/#{session_name}_#{window_name}_#{window_index}_#{pane_index}_$(date +%Y-%m-%d-%H%M%S).log\""'

Direct copying to your HOST when using VMware

I noticed an issue when using the xclip selection. It does not want to copy to my host machine.
This issue I have resolved, by changing the following line:

bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -selection clipboard -i'

And change this to:

bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xsel --clipboard --input && vmware-user-suid-wrapper'