111

Now for copy to system clipboard I have to select via mouse text in tmux window with Shift key. And then I have to run this command:

tmux save-buffer - | reattach-to-user-namespace pbcopy

Is any chance to save to system clipboard easier? Key bind or better do that automatically after Shift release.

My tmux config: https://gist.github.com/3641580

tomekfranek
  • 6,852
  • 8
  • 45
  • 80
  • Here is a similar Q&A on [unix.stackexchange](http://unix.stackexchange.com/questions/15715/getting-tmux-to-copy-a-buffer-to-the-clipboard). – crw Sep 05 '12 at 18:42
  • 2
    Any of these work on MacOS, specifically Sierra? Or rather, none of them do, does anyone know what would? This question is tagged with `osx`, but answers are talking about left- and middle-click... – dwanderson Jan 17 '17 at 19:58

17 Answers17

218

If you are using iTerm2, you can copy text in Tmux session, holding down the Option key while dragging the mouse to make selection.

Then it should be possible to paste text anywhere with Cmd + V as usual. Found it here: http://web.archive.org/web/20131226003700/http://ootput.wordpress.com/2013/08/02/copy-and-paste-in-tmux-with-mouse/

jomuller
  • 1,032
  • 2
  • 10
  • 19
Rolands Bondars
  • 3,087
  • 1
  • 19
  • 8
  • 39
    Doesn't help with tmux panes – Jak S Sep 04 '14 at 11:25
  • 3
    such a simple solution, wish I'd know/googled/figured out a year ago. – Garrett Davis Nov 07 '14 at 18:54
  • 48
    If you have panes, it's possible to make rectangular selection using + + mouse selection. – jomuller Jan 07 '15 at 18:38
  • @jomuller thanks a lot! alt-drag appears to work in urxvt too – Austin Adams May 10 '15 at 22:38
  • 7
    Rectangular selection does not provide the same functionality for panes since the selection can start and finish at any column. This solution is not related to tmux but to the terminal itself. Panes is a core feature of tmux. – Betamos Nov 03 '15 at 00:24
  • 7
    @JakS prefix+Z zooms the current pane. Then you won't have that problem. Unless you're also inside a vim split :) – dgmora Oct 15 '16 at 14:41
  • 1
    Can someone explain what the Options key is? – lony Apr 10 '17 at 09:00
  • 2
    @lony The Option key is labeled Alt on a Mac keyboard. – Rolands Bondars Apr 11 '17 at 09:40
  • 11
    In Terminal, you can select text by holding down "fn" key. You can copy the selected text by "Cmd+C". – fbessho May 07 '17 at 09:03
  • you might still need to give permissions to access clipboard but for me iTerm opened a box telling me that so I didn't have to guess what was wrong. Thanks iTerm! :D Thought his answer was still useful and work reading: https://stackoverflow.com/a/38849483/1601580 since it doesn't take you to the selection box. – Charlie Parker May 04 '21 at 16:01
82

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 macOS clipboard! No need to press Option as suggested in the accepter answer.

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

Jerome Dalbert
  • 10,067
  • 6
  • 56
  • 64
  • 1
    All of the `reattach-to-user-namespace`-based approaches don't work for me, because I often ssh into linux machines. This solution, however, works great! Thank you! – jdg Dec 08 '16 at 20:35
  • Great Answer. I still use Option when I'm in vim and don't want the mouse to activate a vim selection. – Chad Skeeters Mar 30 '17 at 16:28
  • This worked beautifully. No need to change any configuration. Thanks. – muammar Feb 09 '18 at 17:58
  • this works great with just tmux, but now how to send vim yanks running within that tmux to the iterm and system clipboards?? – schlow Dec 04 '19 at 21:35
  • @schlow This should probably be a separate Vim+tmux StackOverflow question but I happen to use Vim too and I added `set clipboard=unnamed` in my vimrc for that purpose – Jerome Dalbert Dec 06 '19 at 00:25
  • Good solution, but now every marked text will be copied to the clipboard – greenchapter Apr 07 '21 at 07:39
51

For os x, If you use app "Terminal". You can switch off mouse scroll and use usual copy/paste functions (cmd+c/cmd+v):

set-option -g mouse-select-pane off
set-option -g mouse-resize-pane off
set-option -g mouse-select-window off
set-window-option -g mode-mouse off

or you can use mouse scroll and select text using "fn key":

set-window-option -g mode-mouse on
Sergei K
  • 967
  • 11
  • 12
  • 41
    the `fn` key tip is a life saver! a lot of people suggest using `ALT` but it doesn't work on latest Mac OS x – AK_ Jul 13 '17 at 07:59
  • 3
    Better yet - combine _fn_ and _option_ to select using column mode! – Fletch Oct 05 '18 at 17:51
  • 1
    Any way to change the fn key? Using an external keyboard that doesn't have the fn key! – CentAu Nov 25 '18 at 18:25
  • @CentAu Try using "Window" key instead – Piotrek Zatorski Jul 10 '20 at 10:13
  • 1
    Cool! For now, need to `set-option -g mouse off`. – lk_vc Jun 11 '21 at 03:06
  • Bro.. the FN key trick is THE BEST ANSWER! You should rewrite your answer so that the FN solution stands out more because I think it will be the correct solution for most users on macs. I tried so many other suggestions with xclip etc. nothing works on modern macs while this solution is not just working out of the box but it is also the SIMPLEST! – jule64 May 09 '23 at 12:20
30

There is a similar question answered here: https://unix.stackexchange.com/questions/15715/getting-tmux-to-copy-a-buffer-to-the-clipboard

Solution found there:

# move x clipboard into tmux paste buffer
bind C-p run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"
# move tmux copy buffer into x clipboard
bind C-y run "tmux show-buffer | xclip -i"

Check also: http://awhan.wordpress.com/2012/04/18/tmux-copy-paste-with-mouse/

if you have set the following options:

mouse-select-pane

mouse-select-window

you will notice that you are not able to select text at all.

the solution is to use the shift key.

hold down the shift key and then left click and drag across the target text. you must also hold down the shift key and then middle click in order to paste the text.
Community
  • 1
  • 1
Blaise
  • 7,230
  • 6
  • 43
  • 53
  • 4
    with iterm, use the option key instead of the shift key. [as mentioned here: http://awhan.wordpress.com/2012/04/18/tmux-copy-paste-with-mouse/ ] – gatoatigrado May 22 '13 at 18:27
  • 3
    For MacOS, `xclip -o` => `pbpaste`, `xclip -i` => `pbcopy`. – Gingi Sep 14 '16 at 16:28
24

On OSX 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"

For tmux version 2.4, since they kindly decided to change the command syntax, you should use:

bind-key -T copy-mode MouseDragEnd1Pane send -X copy-pipe-and-cancel "pbcopy"

Note that you may also need to use reattach-to-user-namespace pbcopy instead of plain pbcopy.

shawkinaw
  • 3,190
  • 2
  • 27
  • 30
Kyle Kloepper
  • 1,652
  • 14
  • 8
12

Putting this in my ~/.tmux.conf file works for me:

bind y run-shell "reattach-to-user-namespace -l zsh -c 'tmux show-buffer | pbcopy'"

All other potential solutions I found were copying some sort of empty content to the OSX clipboard.

The only problem is that this seems to put a carriage return at the end of the pasted content.

crcastle
  • 630
  • 5
  • 8
  • 3
    This works, but still requires you to press PREFIX+y after you made the selection. Is there a way to copy text from a tmux session in OS X just by selecting it like in a Linux terminal? – Rafael Bugajewski Aug 02 '15 at 10:38
6

press shift key and then mouse click!! Check this link. http://awhan.wordpress.com/2012/04/18/tmux-copy-paste-with-mouse/

user3023768
  • 87
  • 1
  • 1
6

press the left mouse button and hold it, select text in pane and still hold the button down. then press "y", which saves selection within clipboard and escapes from selection/copy mode.

works here with iTerm 3.1.beta.5, tmux 2.5 and the following config:

### copy & paste -------------------------------------------------------
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Setup 'v' to begin selection as in Vim
bind-key -T copy-mode-vi v send -X begin-selection
bind-key -T copy-mode-vi y send -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"
bahnfrei
  • 61
  • 1
  • 1
6

I was able to solve this problem with CMD+C after selecting the text with FN+mouse drag with left button down from tmux window in Terminal app of macOS High Sierra.

lemmaa
  • 61
  • 1
  • 1
4

Here's the solution I'm using for tmux version 2.8, macOS Mojave 10.14.1, and Terminal.app.

After releasing the mouse button, this removes the selection while copying to the system clipboard (as well as the tmux buffer):

bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"
robenkleene
  • 572
  • 8
  • 17
3

For those looking for how the selection works in tmux now in mac: use "fn" key to select, copy and paste.

Abrar A.
  • 113
  • 3
  • 11
2

A robust solution that works with any combination of mosh + tmux + vim + nested sessions

After spending many days tackling this, by far the most robust solution I've discovered is Suraj N. Kurapati's posted solution. The solution works locally with (and without) tmux v3.2 and/or vim, as well as remotely with (and without) tmux and/or vim, using both copy-mode keys and simple mouse selection.

If combined with Mike Gulick's (currently open) pull request, all of these combinations work with mosh as well.

Until Mike's solution is merged into mosh, you can pull his changes and build locally using:

Ubuntu:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0 \
&& sudo apt-add-repository https://cli.github.com/packages \
&& sudo apt update \
&& sudo apt install -y gh autoconf automake perl protobuf-compiler libprotobuf-dev libncurses-dev zlib1g-dev libutempter-dev libssl-dev pkg-config \
&& mkdir -p ~/tmp/mosh \
&& git clone https://github.com/mobile-shell/mosh ~/tmp/mosh \
&& cd ~/tmp/mosh \
&& gh pr checkout 1104 \
&& ./autogen.sh \
&& ./configure \
&& make \
&& sudo make install

macOS:

I think you need to install Apple's Xcode/Developer Command Line Tools first (which, IIRC, can now be done by simply running gcc and exiting), then running:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" \
&& brew install gh autoconf automake
&& mkdir -p ~/tmp/mosh \
&& git clone https://github.com/mobile-shell/mosh ~/tmp/mosh \
&& cd ~/tmp/mosh \
&& gh pr checkout 1104 \
&& ./autogen.sh \
&& ./configure \
&& make \
&& sudo make install

With these updates, the clipboard holy grail has finally been achieved!

However, this is very new information, thus this post of mine is rather volatile. Therefore:

Please kindly revise my post if:

  1. I'm missing dependencies, etc, or
  2. Performing a gh pr checkout 1104 of Mike's changes is no longer needed because it's been merged into mosh, or
  3. Building from source is no longer needed because the merged changes have been pushed to brew/apt/etc package managers.
2

monterey macosx terminal 2.21 with default settings connected to a machine over ssh with tmux 2.6 running on it, with several panes, tmux config there is as simple as that:

set -g mouse on

To copy - hold the Fn button and select what you need using mouse, then press ⌘ Command + C, et voila, the text is in the buffer on your mac.

Ilya Sheershoff
  • 409
  • 4
  • 10
2

I cannot believe none of old answers works for me, I grep tmux source code, and find the solution is really simple:

bind -Tcopy-mode M-w send -X copy-pipe-and-cancel "pbcopy"

I'm a Emacs fans, so of course use the tmux's default Emacs keybindings, which can be found here:

https://github.com/tmux/tmux/blob/1a11c972aed11c1996d732f15ad16f02c668b5a0/key-bindings.c#L491

If you are using vi keybinding, try this

bind -Tcopy-mode-vi Enter send -X copy-pipe-and-cancel "pbcopy"

Tested in Terminal.app on tmux 3.3a and macOS 13.4 (22F66).

There is also a wiki page for this

PS: Sometimes we can just RTFSC instead of googling hours

Jiacai Liu
  • 2,623
  • 2
  • 22
  • 42
1

I made a version using netcat today that is pretty dead simple.

https://github.com/base698/tmux-copy-paste-mac-osx

Here is the tmux.conf:

bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-pipe 'nc localhost 5682'
bind p run-shell 'nc localhost 5683; nc localhost 5683 | tmux load-buffer -' \; paste-buffer

Works with the mouse too in 1.8

Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
1

On macOS Terminal Tmux, copying from a specific pane:

  1. Hold option (cursor changes to crosshair) [See * below]
  2. Click and drag to select text
  3. Copy/paste as normal, with CMD-C/V

*You may need to toggle "Allow mouse reporting" with CMD-R (see Terminal, View-...). I find that I often toggle this to either scroll with mouse inside tmux panes, or to copy-paste.


In my .tmux.conf, I include set -g mouse on

This is on macOS Catalina, tmux 3.1c

Paul
  • 3,920
  • 31
  • 29
0

The latest suggestion I found in https://gist.github.com/brendanhay/1769870 worked on my Mac and terminal windows. Both simple mouse selection and keyboard vi-style worked.

# tmux 2.6 doesn't need the 'reattach-to-user-namespace' gymnastics
setw    -g  mode-keys    vi
bind-key -T edit-mode-vi Up                send-keys -X history-up
bind-key -T edit-mode-vi Down              send-keys -X history-down
bind-key -T copy-mode-vi v                 send      -X begin-selection
bind-key -T copy-mode-vi [                 send-keys -X begin-selection
bind-key -T copy-mode-vi y                 send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi Enter             send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi ]                 send-keys -X copy-selection
WeakPointer
  • 3,087
  • 27
  • 22