87
votes

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

14
Here is a similar Q&A on unix.stackexchange. - crw
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

14 Answers

193
votes

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/

64
votes

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.

30
votes

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
29
votes

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.
20
votes

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.

11
votes

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.

6
votes

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"
5
votes
3
votes

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.

2
votes

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"
1
votes

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

1
votes

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.
1
votes

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

0
votes

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