Copying and pasting text in tmux
June 28, 2020
- Hold down
Shift(orOptionon macOS) key and select text with mouse to copy - Hold down
Shift(orOptionon macOS) key and press Mouse middle-button to paste
The above works. But if you have multiple panes, it'll select text from all panes instead of staying inside the bounderies of one pane. To fix that, we need to edit our ~/.tmux.conf file
macOS
code
# Copying text in Mouse mode
# https://unix.stackexchange.com/a/318285
set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-vi C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"
# Highlight some text with mouse, but don't let go the mouse. Now while the text is still highlighted (in yellow) and mouse pressed, press return key. The highlighted text will disappear and will be copied to your clipboard. Now release the mouse.
Linux
code
sudo apt install xclip
code
# Copying text in Mouse mode
# https://unix.stackexchange.com/a/318285
# Needs `xclip` on Linux. sudo apt install -y xclip
set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-vi C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -selection c"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
# Highlight some text with mouse, but don't let go the mouse. Now while the text is still highlighted (in yellow) and mouse pressed, press return key. The highlighted text will disappear and will be copied to your clipboard. Now release the mouse.
- The macOS version uses
pbcopywhile the Linux version usesxclip. You will need to installxclipon Ubuntu - To copy, left click and drag to highlight text in yellow, press Enter and then release mouse.
Ctrl + Shift + Vto paste
DRAFT Writing one config for both Linux and macOS
Get the system you're on with uname -s. Ubuntu gives Linux while macOS gives Darwin
NOTE: Bash variables and conditionals don't work inside a tmux conf file. Update these with if-shell or run-shell to get uname result, and then %if ... %else ... %endif to write the confitional
code
MACHINE=$(uname -s | tr '[:upper:]' '[:lower:]')
Once you have the system name, you can use an if/esle or case statement
code
[[ $MACHINE = linux ]] && "xclip -selection c" || "pbcopy"
[[ $MACHINE = linux ]] && "xclip -in -selection clipboard" || "pbcopy"
code
sudo apt install -y xclip
code
echo -e "
# Detect the platform we're running on (`Linux` for Ubuntu, `Darwin` for macOS)
MACHINE=$(uname -s | tr '[:upper:]' '[:lower:]')
# Copying text in Mouse mode
# https://unix.stackexchange.com/a/318285
# Needs `xclip` on Linux. sudo apt install -y xclip
set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-vi C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel [[ $MACHINE = darwin ]] && "pbcopy" || "xclip -selection c"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel [[ $MACHINE = darwin ]] && "pbcopy" || "xclip -in -selection clipboard"
# Highlight some text with mouse, but don't let go the mouse. Now while the text is still highlighted (in yellow) and mouse pressed, press return key. The highlighted text will disappear and will be copied to your clipboard. Now release the mouse.
" >> ${HOME}/.tmux.conf
code
%if #{==:#{host},myhost} "pbcopy" %else "xclip -selection c" %endif
%if #{==:#{host},myhost} "pbcopy" %else "xclip -in -selection clipboard" %endif