TheBlackzone Logo

TMUX and the system clipboard

Posted on Sunday, August 14, 2022 by TheBlackzone

When I started using TMUX years ago, I frequently updated it from its Github repository, always eager to learn about the latest features of each new version. But, having become the lazy slacker I am nowadays and also having become quite comfortable with my current tmux setup, I haven’t done so for ages.

Today however, I finally did an upgrade to the latest git version and took the opportunity to fix my TMUX configuration regarding a (mildly annoying) shortcoming of not being able to copy mouse selection directly to the system clipboard.

I normaly use the keyboard to copy and paste from or to tmux (CTRL-B being my tmux prefix key):

This behaviour is achieved by these configuraton options:

# Use vi keys for navigation
set-window-option -g mode-keys vi
# begin selection
bind -T copy-mode-vi v send-keys -X begin-selection
# copy to global clipboard
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -i -f -selection prima
# paste from global clipboard
bind p run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"

One thing that had never worked for me however was, to copy an area of text from tmux directly to the system clipboard by using the mouse.

To be honest, this had not bothered me enough to put a lot of energy in finding a solution for it, because in my terminal of choice (xfce4-terminal) it is easily possible to just hold down the shift key when doing mouse selections and then shift-right-click to use the terminal’s native copy-paste functionality.

Nevertheless, with the new version of TMUX I finally found an easy solution by adding these settings to my configuration file:

set-option -g mouse on
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard >/dev/null"

It’s the same command as for copying by keyboard, except the keycode being ‘‘MouseDragEnd1Pane’’.

I actually don’t know for how long this solution has been around, but I’m glad I finally learned about it. My TMUX experience now seems to be complete :-)

And, since everybody seems to enjoy looking at other’s configuration settings, here’s my complete ‘'.tmux.conf’' file:

# TERM
set -g default-terminal "screen-256color"

# Statusbar
set-option -g status on
set-option -g status-bg colour235
set-option -g status-fg colour244
set-option -g status-interval 2

set -g status-right '#[fg=green]#[bg=green]#[fg=black]  #(hostname)  %Y-%m-%d %H:%M #[fg=colour17]#[bg=colour17]#[fg=colour39] #(whoami) #[default]'

# Highlight active window
set-window-option -g window-status-current-style fg=white
set-window-option -g window-status-current-style bg=colour235

set-option -g pane-border-style bg=colour235
set-option -g pane-border-style fg=yellow
set-option -g pane-active-border-style fg=colour244

# key bindings
set-window-option -g mode-keys vi
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

bind r source-file ~/.tmux.conf \; display-message "Config reloaded..."
bind v split-window -h
bind b split-window -v

bind-key C-b last-window
bind C-v paste-buffer

# begin selection
bind -T copy-mode-vi v send-keys -X begin-selection
# copy to global clipboard
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard >/dev/null"
# paste from global clipboard
bind p run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"

# options
set-window-option -g clock-mode-colour colour33
set-window-option -g aggressive-resize on

# faster key repeat
set -s escape-time 0

# Activity monitoring
set-window-option -g monitor-activity on
set -g visual-activity on

# mouse support
set-option -g mouse on
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard >/dev/null"

# scrollback
set -g history-limit 128000

# tab numbering starts at 1
set -g base-index 1

# F1 bound to quickly create a multi pane layout for my development work
bind F1 split-window -v -l 8 \; split-window -h -p 50 \; split-window -h -p 50 \; select-pane -t 0 \; split-window -h -p 50

You may have noticed those “special characters” in the status bar setup. These are actually some modified unicode characters coming from the font I use for my terminal, “LiterationMono Nerd Font”, which can be found here.

Tags: linux

Button PreviousMicrosoft Teams Introductory Links

Adjusting the Linux (Xfce) Volume ControlButton Next