tmux.conf with clipboard integration & extra features

I have been using the terminal multiplexer tmux for almost two years now, but I never used its mouse support. The big reason was that it messed up the native terminal copy-paste support.

I spent some time reading the tmux(1) man page, and finally got something working that's comparable to gui terminal emulators like gnome-terminal.

Nice things I added here:

  • Copy mode selection with keyboard/mouse will copy into the clipboard, rather than just the tmux buffer.
  • Right click to paste from the clipboard.
  • Middle click on the window label to close a window.
  • Double click on any window label to open a new window.
  • Drag to re-order windows.

For those who are not familiar with tmux terminology, a tmux "window" is comparable to a browser "tab".

Here's the relevant tmux.conf parts annotated (my actual tmux.conf is here):

#!tmuxsh
# Enable mouse support.
set -g mouse on
# Middle click on the window label to kill it
# "=" is apparently a macro for the "selected window number" but only for 
# a certain set of commands.
bind-key -n MouseUp2Status kill-window -t=
# Drag to re-order windows
bind-key -n MouseDrag1Status swap-window -t=
# Double click on the window list to open a new window
bind-key -n DoubleClick1Status new-window

## Clipboard integration (only applicable when you have an X server running)

# Selection with mouse should copy to clipboard right away, in addition to the default action.
# Unbind the default action first.
unbind -n -Tcopy-mode-vi MouseDragEnd1Pane
bind -Tcopy-mode-vi MouseDragEnd1Pane send -X copy-selection-and-cancel\; run "tmux save-buffer - | xclip -i -sel clipboard > /dev/null"
# Copy mode copy should also copy it to the clipboard as well.
unbind -Tcopy-mode-vi Enter
bind -Tcopy-mode-vi Enter send -X copy-selection-and-cancel\; run "tmux save-buffer - | xclip -i -sel clipboard > /dev/null"
# Right click to paste from the clipboard
# If you like middle click better, change MouseDown3Pane to MouseDown2Pane for middle click.
unbind-key MouseDown3Pane
bind-key -n MouseDown3Pane run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"

A few interesting things to note:

  • The equals sign (=) in kill-window is a macro for the selected window number. But, it only works with specific commands, in an unescaped form. You can't do confirm-before "kill-window -t=". Don't waste your time trying to get it working.
  • You must redirect xclip output into /dev/null, or tmux will hang (Relevant SO link).
  • Also, If you are not using the vi key binding, all the un/binds with copy-mode-vi in it, needs to be replaced with copy-mode and their respective key binding. The default tmux key binding is actually hard-coded in their source code.
  • Notably, this seems to work pretty well on the latest release of Windows 10 Bash on Ubuntu on Windows / WSL (Wow, what a mouthful name!). Just run an X server like Xming and your clipboard will integrate with the Windows clipboard. Now I can use tmux full-time on Windows.

What can be improved:

  • Disable the clipboard integration if X is not available. Since I always work with X, I didn't think this was useful.
  • It does not select a word like on double click.
  • Right after selection, it exits the copy-mode. It's kind of jarring.

Screenshot (well.. it doesn't look any different with mouse support)

Catergorized under: techtips / tmux

Published: 2017-04-16T02:45:40.881650
Last modified: 2017-04-16T03:57:18.963295
Permalink