24

I am quite happy with the Terminal changes in El Capitan (see e.g. this question).

However, i can't figure out how to enable copying to system clipboard from within Tmux. For instance, now that I am able to select text within Tmux properly by mouse, how can i copy selected text to clipboard as well, in addition to Tmux's own buffer?

I have the following in my tmux.conf:

setw -g mode-mouse on
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on

setw -g mode-keys vi
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

I tried modifying the last line to

bind-key -t vi-copy 'y' copy-selection \; copy-pipe "pbcopy"

but it didn't really work. Would be grateful for suggestions!

Thanks!

3
  • Cmd-C doesn't work?
    – nohillside
    Oct 2, 2015 at 9:04
  • 1
    @partix - no, as copied text is auto-deselected after releasing the mouse button (or trackpad). Of course it does work in Terminal, but not from Tmux within terminal.
    – siphiuel
    Oct 2, 2015 at 9:21
  • Update to Tmux 2.1 broke mouse scrolling in Tmux panes.
    – siphiuel
    Oct 19, 2015 at 17:35

9 Answers 9

39

Actually, I just find out very simple solution how to enable copying using Cmd+C again.

In default Terminal application go to View->"Allow mouse reporting" and uncheck it. Thats it.

PS: Obviously, the new nice features like dragging split screen in tmux stop working after unchecking the mouse reporting.

5
  • PS: I used previously solution from @siphiuel, which works also fine.
    – Oplatek
    Oct 28, 2015 at 3:46
  • 1
    Thank you!!! This is the simplest way! Only that the history is broken.I can only see part of hitstory
    – Sato
    Mar 24, 2016 at 23:54
  • Super nice. This works
    – ShikharDua
    Jan 30, 2017 at 18:59
  • 1
    and this also works for a remote tmux session; I'm thinking that the bind-key ... copy-pipe solution might only work if it's a local tmux session. Then again, maybe if you do the remote tmux session within a local tmux session, it might work? Anyway, unchecking "Allow mouse reporting" worked for me, and there's even a shortcut ⌘-R (Cmd-R) so that you can quickly toggle it off to make a copy, then back on again. Mar 9, 2017 at 21:30
  • Thanks for the ⌘-R (Cmd-R) shortcut - that helps a lot!
    – emvee
    Mar 30, 2021 at 16:33
25

If you use iTerm2 3.x, you can make copy / paste work by enabling Applications in terminal may access clipboard:

enter image description here

Just highlight text with your mouse to copy it into the OS X clipboard!

You can also paste from the OS X clipboard into tmux with the usual Cmd + V.

2
  • 2
    this was the easiest solution for me. Feb 23, 2017 at 18:23
  • This is awesome!
    – mljrg
    Dec 12, 2017 at 11:52
23

If you hold the function key down you can select and copy to clipboard with apple+c as well.

3
  • We are looking for something more than a single line answer. Try expanding your answer to give further detail.
    – Allan
    Apr 19, 2016 at 22:38
  • 6
    @Allan This is a complete answer, even though it's just a single line. It's that simple.
    – pkuhar
    Apr 18, 2019 at 16:27
  • btw, I found it only works on the laptop keyboard, not attached keyboards :(
    – amohr
    Apr 21, 2019 at 6:27
10

Actually, i was wrong when stating that copy-pipe does not work. It does, and copy-selection is then superfluous, as copy-pipe both copies text to Tmux pasteboard and pipes to an arbitrary shell command. So the following line works:

bind-key -t vi-copy 'y' copy-pipe "pbcopy"

However, one needs to press y before releasing the mouse button. I've stumbled across this in the documentation for tmux-yank plugin:

"When making a selection using tmux mode-mouse on or mode-mouse copy-mode, you cannot rely on the default 'release mouse after selection to copy' behavior. Instead, press y before releasing mouse."

So it works, but with somewhat annoying need to press y in order to copy mouse-selected text.

2
  • 3
    If you are using Terminal.app: in macOS Sierra this won't work due to regression in the Terminal.app behaviour. You will need to use "reattach-to-user-namespace" hack again.
    – m1keil
    Sep 25, 2016 at 3:15
  • 1
    Agree on @m1keil comment. Moreover, for some reason i had to compile reattach-to-user-namespace with additional switches, like this: brew install reattach-to-user-namespace --with-wrap-pbcopy-and-pbpaste (i am using Homebrew)
    – siphiuel
    Sep 26, 2016 at 11:28
5

Using tmux version 2.2 or later add the following to your tmux.conf file:

bind-key -t emacs-copy MouseDragEnd1Pane copy-pipe "pbcopy"
bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "pbcopy"
1
  • Works for El Capitan 10.11, tmux 2.7
    – Homer
    Jun 16, 2018 at 5:05
5

The answer of @Oplatek take effect

In default Terminal application go to View->"Allow mouse reporting" and uncheck it.

But by this way, we would not be able to scroll the screen by mouse.

Here's another way without disable Allow mouse reporting:

Press Fn and select the text by mouse. It keeps the selection there so you can right click and select copy.

1

Using tmux 2.2, OS X El Capitan, Terminal Version 2.6.1 (the default that ships with El Capitan), the following setup works flawless (so far).

$ cat ~/.tmux.conf
set -g mouse on
bind-key -t emacs-copy MouseDragEnd1Pane copy-pipe "pbcopy"
bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "pbcopy"

To copy, press the left mouse button, mark the text, and let go. The marked text is then in the clipboard and can be pasted with Cmd + v as usual.

0

Check ChrisJohnsen/tmux-MacOSX-pasteboard: Notes and workarounds for accessing the Mac OS X pasteboard in tmux sessions.

brew install reattach-to-user-namespace

Then add to .tmux.conf:

set-option -g default-command "reattach-to-user-namespace -l zsh"
-2

i find only one way

open terminal settings/profiles in terminal info change terminal type from xterm-256 to ansi open new terminal window and enjoy :)

You must log in to answer this question.

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