121

I'm using tmux and OSX. When copying and pasting from the terminal with tmux I'm able to hold down Option and select text. However I can't get the text to stay inside the pane. So when I want to copy text I either need to cycle the pane to the far left, or zoom the pane, as shown below.

This in addition to having to hold down the Option key is a pain. I know I can enter visual mode and use vim movements to get there, but I'd rather have a way to use my mouse. Has anyone found a workaround for this?

enter image description here enter image description here

2

7 Answers 7

118

Put this block of code in your ~/.tmux.conf. This will enable mouse integration letting you copy from a pane with your mouse without having to zoom.

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 vi-copy    C-WheelUpPane   halfpage-up
bind -t vi-copy    C-WheelDownPane halfpage-down
bind -t emacs-copy C-WheelUpPane   halfpage-up
bind -t emacs-copy C-WheelDownPane halfpage-down

# To copy, drag to highlight text in yellow, press Enter and then release mouse
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "pbcopy"

After that, restart your tmux session. Highlight some text with mouse, but don't let go the mouse. Now while the text is stil highlighted and mouse pressed, press return key. The highlighted text will disappear and will be copied to your clipboard. Now release the mouse.

Apart from this, there are also some cool things you can do with the mouse like scroll up and down, select the active pane, etc.

If you are using a newer version of tmux on macOS, try the following instead of the one above:

# macOS only
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

# To copy, left click and drag to highlight text in yellow, 
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # 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"

If using iTerm on macOS, goto iTerm2 > Preferences > “General” tab, and in the “Selection” section, check “Applications in terminal may access clipboard”.

And if you are using Linux and a newer version of tmux, then

# Linux only
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

# To copy, left click and drag to highlight text in yellow, 
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # 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"

In Debian and Debian based distros (Ubuntu, Kali), you might need to install xclip:

sudo apt-get install -y xclip

(You may also check out https://github.com/gpakosz/.tmux for many other tmux options.)

22
  • 5
    Finally something that actually works! Copying and pasting is the single most annoying thing with trying tmux, I've given up on it before and the config snippets I was trying out were behaving inconsistently, thanks a lot man. Commented Apr 9, 2019 at 13:39
  • 2
    I was using iterm in mac and had to enable iTerm2 > Preferences > “General” tab, and in the “Selection” section, “Applications in terminal may access clipboard”. Reference: rushiagr.com/blog/2016/06/16/… Commented Apr 19, 2019 at 18:14
  • 5
    Thanks! Works like a charm. I needed to install sudo apt-get install -y xclip (Ubuntu Linux) though.
    – Petr
    Commented Apr 25, 2019 at 11:41
  • 3
    This worked for me. But I find this so curious how much config this requires. I feel like just having tmux-yank or something would do it. If anyone has a very minimal config with tmux yank and mouse support I'd be interested...I cannot for the life of me figure out why that doesn't work for me (note: my setup includes ubuntu 18, tmux 2.6, running inside zsh 5.4.2 [the tmux-yank mentions something about bindkey -e for zsh?), on default terminal)
    – Colin D
    Commented Aug 1, 2019 at 3:06
  • 2
    You don't need to restart the tmux session. You can simply execute source-file ~/.tmux.conf while the session is on. I surely don't want to close my ten sessions each with 3 windows, with each window containing multiple panes.
    – Peaceful
    Commented Aug 31, 2021 at 8:54
22

Add this 2 lines in ~/.tmux.conf

set -g @plugin 'tmux-plugins/tmux-yank'
set -g mouse on

and then install the plugin works for me.

https://github.com/tmux-plugins/tmux-yank

4
  • 10
    I also had to add this: set -g @yank_selection_mouse 'clipboard'. It will copy to clipboard when you select with mouse. Commented Jan 7, 2022 at 22:30
  • 3
    This comment is the answer I was looking for. Should be promoted.
    – stanm
    Commented Jan 29, 2023 at 8:23
  • doesn't work on macos
    – chovy
    Commented Jan 31, 2023 at 11:09
  • 1
    For macOS tmux users with mouse support enabled, even if setting up tpm adds a bit of friction, IMO this gives the best results. xterm style 'select to copy' - lovely. Works great on macOS 13.2.1 (22D68)
    – paws
    Commented Mar 23, 2023 at 20:02
17

What worked for me in Ubuntu 20.04

  • I have mouse mode on
  • Selected the text while holding Shift + Selecting with cursor
  • Release Shift and mouse once you select the text
  • Copy the text Ctrl+Shift+c
  • Paste any where (outside of tmux itself) Ctrl+v, since this copies the text on your system clipboard
2
  • Also works on Debian 11 with tmux 3.1c
    – Xapadoan
    Commented Mar 7 at 13:54
  • Works for tmux 1.8 on RHEL 7 if you don't have authorization from your IT to upgrade either!
    – mdisibio
    Commented May 24 at 21:12
10

tmux has its own internal clipboard, which is not copied to the system clipboard.

Since you are on macOS, you can however simply ask tmux to transfer its clipboard to the system clipboard (the usual one). This is done with the command below, which, for convenience, can be bound to Ctrl-b Ctrl-c (if you're using the standard Ctrl-b for invoking tmux commands) in your .tmux.conf:

bind C-c run "tmux save-buffer - | pbcopy"  # Copy to macOS

Thus, you can simply select text with your mouse while using tmux (no key to press). tmux is great in that it will select text only in the current pane (even when you have multiple panes side by side). This text is copied to tmux's internal clipboard. You can then simply transfer it to the system clipboard with Ctrl-b Ctrl-c.

1
  • This is a nice solution in that it uses only built-in tools, but the downside is requiring two steps. Casper's answer lets you avoid the 2nd step for the downside of setting up a plugin. For something I do all the time I prefer one step.
    – paws
    Commented Mar 23, 2023 at 20:05
2

On FreeBSD you can use y key (vi-style) to copy selected text while holding the left mouse button on selected text when mouse on is set in ~/tmux.conf :-)

set -g mouse on
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'

Just remember to install (as root) xclip package with pkg install xclip :-)

Some more more useful hints:

  • Use set -g repeat-time 0 will disable following tmux command keys when you have an editor in one pane and shell or other cursor operated applications in the other panes. Usually following cursor keys will switch between panes for some time and that may be distracting and annoying in come cases if you want to use cursor quickly in the pane that you have switched to.
  • Use set -g status-style fg=white,bg=green to easily differentiate between different sessions on different remote machines. Setting different bg value on different machines will help you quickly identify the machine you want to work on :-)
1

For just copying a text in tmux, you can highlight the text you want to copy by holding down left mouse button and then click right mouse button.

Control+v to paste or Control+Shift+v to paste in terminal.

1
  • 2
    Doesn't work for me
    – Nik
    Commented Feb 3, 2023 at 8:43
-1

So what worked for me was to highlight the target text without releasing the left-click on my touchpad or mouse, then doing CMD + c, then releasing the left-click. Now the highlighted text is in my clipboard and doing CMD + v pastes it as expected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .