115

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

mbigras
  • 2,928
  • 6
  • 28
  • 45
  • *"However I can't get the text to stay inside the pane."* What do you mean by this? – Blaszard Jul 05 '18 at 16:21
  • Does this answer your question? [tmux: Select and copy pane text with mouse](https://unix.stackexchange.com/questions/478922/tmux-select-and-copy-pane-text-with-mouse) – Abdull Oct 28 '22 at 09:45

6 Answers6

109

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.)

GMaster
  • 5,992
  • 3
  • 28
  • 32
  • 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. – Breno Salgado Apr 09 '19 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: http://www.rushiagr.com/blog/2016/06/16/everything-you-need-to-know-about-tmux-copy-pasting/ – Rujoota Shah Apr 19 '19 at 18:14
  • @RujootaShah Yes, that's correct. You need to do that. Thanks for sharing. – GMaster Apr 20 '19 at 11:11
  • 5
    Thanks! Works like a charm. I needed to install `sudo apt-get install -y xclip` (Ubuntu Linux) though. – Petr Apr 25 '19 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 Aug 01 '19 at 03:06
  • @Petr , this worked great for me too on Ubuntu 18.04 and tmux 2.6 , and thanks for the tip about `xclip`. It's worth noting that I had to kill all tmux sessions before the edit of `.tmux.conf` was being used by tmux. – evaristegd Mar 05 '20 at 18:20
  • 1
    Does not work on Xenial with tmux 2.1. – user292812 Apr 08 '20 at 09:50
  • this worked for me, macOS 10.14 using bash and tmux 2.9a – Ilias Karim Apr 13 '20 at 16:49
  • How can I change it to use left click instead? Like putty is on windows – Freedo Sep 14 '20 at 05:30
  • laptops like mine with touchpads don't have a middle button – Freedo Sep 14 '20 at 05:32
  • @Freedo Three finger tap might simulate a middle mouse click. – GMaster Nov 12 '20 at 11:08
  • `gpakosz/.tmux` repository was a good idea and a prepared configuration but I cannot use mouse toggle and copy-text simultaneously. In this way, I have to enable mouse toggle with `m` next turn it off to copy a text. Or I can enable `mouse on` on its configuration and turn it off when I want to copy a text. – Benyamin Jafari Jan 10 '21 at 09:54
  • dude, you are godsend! – Uzumaki D. Ichigo May 27 '21 at 20:46
  • 1
    I really don't understand why this answer has so many upvotes. All the work done has nearly nothing to do with the question. It was (in my words) about "selecting text using the mouse without having trouble caused by a 2nd pane". There was no question about copying text to the system's clipboard. `tmux` has its own "clipboard"-buffer which you could fill e.g. after maximizing the pane (`Ctrl`+`b`-`z`) or after setting mouse mode on (`Ctrl`+`b`-`:` and then `set-option -g mouse on`). Then you just press `Ctrl`+`b`-`]` to insert this buffer in some other pane. – Gerd May 28 '21 at 08:58
  • 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 Aug 31 '21 at 08:54
  • u da man '''''' – Sam Berry Oct 18 '21 at 20:16
  • This answer copies the highlighted text in the tmux pane to the ctrl+shift+v clipboard, not the middle-mouse-button clipboard. That's not what I would find intuitive. Changing the last two lines from `-selection c` to `-selection primary` fixes that. – lucidbrot Feb 28 '23 at 11:06
  • cursor movement speed is so so so slow with this configuration – alper Apr 25 '23 at 13:37
  • @alper what is your tmux version? – GMaster Apr 25 '23 at 14:48
  • @GMaster its `tmux 3.3a` – alper Apr 25 '23 at 19:04
16

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

Casper Tsui
  • 161
  • 1
  • 3
  • 8
    I also had to add this: `set -g @yank_selection_mouse 'clipboard'`. It will copy to clipboard when you select with mouse. – Asocia Jan 07 '22 at 22:30
  • 3
    This comment is the answer I was looking for. Should be promoted. – stanm Jan 29 '23 at 08:23
  • doesn't work on macos – chovy Jan 31 '23 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 Mar 23 '23 at 20:02
6

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.

Eric O. Lebigot
  • 403
  • 1
  • 4
  • 10
  • 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 Mar 23 '23 at 20:05
4

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

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 :-)
CeDeROM
  • 287
  • 2
  • 6
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.

Greenonline
  • 1,759
  • 7
  • 16
  • 21