Skip to content

Instantly share code, notes, and snippets.

@issmirnov
Forked from gengmao/gist:75e86c1d9d5427f9632d
Last active July 10, 2020 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save issmirnov/ca43b5f8762825c0c829b0665aebe77a to your computer and use it in GitHub Desktop.
Save issmirnov/ca43b5f8762825c0c829b0665aebe77a to your computer and use it in GitHub Desktop.
Select text from a remote tmux session in mouse mode and copy back to Mac

Forked from: https://gist.github.com/gengmao/75e86c1d9d5427f9632d Original Author: @gengmao Minor changes by @issmirnov, specifically update config for modern tmux and add some notes.

I like to use mouse mode in tmux to scroll text in windows/panes, however I found there is problem when I wanna select and copy text into local system clipboard.

The problem is described at https://awhan.wordpress.com/2012/04/18/tmux-copy-paste-with-mouse/. The workaround is zoom tmux pane -> turn off mouse mode -> select with Option key and copy, then go back. But it is not ideal.

Fortunately I found http://seancoates.com/blogs/remote-pbcopy and https://gist.github.com/burke/5960455. I tweak them a bit and got what I want - select text in one pane in mouse mode correctly and copy it back to Mac system clipboard.

Here are my configurations:

Local (OS X) Side

~/Library/LaunchAgents/pbcopy.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
     <key>Label</key>
     <string>localhost.pbcopy</string>
     <key>ProgramArguments</key>
     <array>
         <string>/usr/bin/pbcopy</string>
     </array>
     <key>inetdCompatibility</key>
     <dict>
          <key>Wait</key>
          <false/>
     </dict>
     <key>Sockets</key>
     <dict>
          <key>Listeners</key>
               <dict>
                    <key>SockServiceName</key>
                    <string>2224</string>
                    <key>SockNodeName</key>
                    <string>127.0.0.1</string>
               </dict>
     </dict>
</dict>
</plist>

~/.ssh/config

RemoteForward 2224 127.0.0.1:2224 

After adding the plist above, you'll have to run:

launchctl load ~/Library/LaunchAgents/pbcopy.plist

Remote (Linux) Side

~/.tmux.conf

# enable mouse. you can use mouse scroll window/pane and select text, but you can't copy text without the copy-pipe setting below.
set -g mode-mouse on
setw -g mouse-select-window on
setw -g mouse-select-pane on
# click mouse and select the text in tmux window/pane, and hit 'y' key to copy it back though a local port opened by ssh.
# - don't release mouse before hit 'y' key. 
bind-key -T copy-mode-vi 'y' copy-pipe "cat | nc -q1 localhost 2224"

I found the pbpaste.plist is unnecessary. In mouse mode, you can paste contents from mac system clipboard into remote tmux session freely (maybe because I am using iTerm2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment