How to copy from tmux running in putty to windows clipboard

26,967

Solution 1

PuTTY is just a terminal emulator; the Vim registers * and + are concerned about the X selection and clipboard; there's no overlap.

PuTTY only allows you to copy the selected terminal contents to the Windows clipboard; when you run tmux, that will inevitably include the window layout.

You need to switch from PuTTY to something that allows real integration, like the Cygwin XWin server, which is a real X Server that integrates the X clipboard with the Windows clipboard. Instead of inside the PuTTY session, you'd ssh -X into your server, and launch Vim in a Linux terminal, or GVIM directly. Then, yanking via "+y will work as you'd expect.

Alternatively, if you want to keep using PuTTY, you'd have to use some workaround, like :writeing the selection to a local file, and transferring that to Windows via scp, for instance.

Solution 2

I use putty v0.62 and tmux v1.8.

tmux config: setw -g mode-mouse on

I want to copy some text from tmux to system clipboard, I press and hold SHIFT and select the text by mouse, then click left-button of mouse.

I want to paste some text into tmux, press SHIFT and click right-button of mouse.

Solution 3

This is absolutely possible. All of the config lines below were copied from machines in different tmux panes back to my Windows machine. Yes, I putty into one machine and tmux and ssh from there to others and can move my vim yanks to any machine, including my Windows browser.

I do run an X server on Windows (doesn't need to be on Windows, but you need one somewhere).

Essential ingredients:

  • putty 0.62 (on Windows)
  • tmux 1.8 (on one Linux machine)
  • xclip (on Linux machines)
  • MobaXterm (on Windows for their x server)

Your network setup might differ a bit, especially on the X server display config but here it goes:

tmux

# allow mouse to select panes
setw -g mode-mouse on
set-option -g mouse-select-pane on

# allow yank into system clipboard
#bind C-y run "tmux save-buffer - | xclip -i"
bind C-y run-shell -b "tmux show-buffer | xclip -selection clipboard -i"
# move x clipboard into tmux paste buffer
bind C-p run-shell -b "xclip -o -selection clipboard | tmux load-buffer - ; tmux paste-buffer"

vim

" get some  autoselect interaction with the system clipboard
set guioptions=aA

MobaXterm

I keep this running but don't ever have to run tmux in a xterm shell. Make sure that the shared clipboard is selected in the X11 settings. I suppose xming should work but the settings for my other needs never seemed to work out. MobaXterm has other goodies I like anyway (file browsers etc).

I suppose I could run ssh session with tmux in MobaXterm but I prefer the putty window where I have my solarized color palette.

putty

ssh to the machine with tmux. At this point, I set the DISPLAY environment variable and put it in an env file that is sourced by bash. This way, every subsequent window created will have it set. Setting it on other machines that I hop to from the tmux machine is a separate exercise entirely.

In any machine that will be sharing the clipboard, make sure you have the proper $DISPLAY set. You should be able to run xeyes and see it on your local machine.

Everyone will be sharing the clipboard. vim will copy to the clipboard when you yank. To paste in another tmux pane, use the bind-key ctrl-p (ctrl-a ctrl-p for me).

If it's not working, you can always try xclip -o -selection clipboard to see what should paste. you also will be able to move any "tmux selected" text (mouse or bind-key [) to the clipboard (and thus to a regular windows window) with bind-key ctrl-y (i.e. tmux yank).

Solution 4

With some trickery, it is possible to get the tmux buffer back through PuTTY and onto the client. I accomplished this using ANSI escape codes for the "AUX" port (serial printer).

Here is just one implementation of that method of transfer:

1) In server-side tmux.conf, add:

# Send the tmux copy buffer to a file.  The file is read for ANSI printing by "t" alias in .bashrc
bind -t vi-copy y copy-pipe 'cat > ~/.tmux-buffer' 

2) In server-side .bashrc, add:

t() {
  # Configure a PuTTY profile to send "t" as the "Remote command".  This
  # function will automatically reattach to an existing tmux session if one
  # exists, or start a new one.  This function also repeatedly sends our
  # homemade tmux clipboard back to the PuTTY client in the form of an ANSI
  # printer escape sequence.  The contents of the homemade clipboard are
  # populated by `bind -t vi-copy y copy-pipe 'cat > ~/.tmux-buffer'` in
  # tmux.conf.  It is expected that the PuTTY client will be configured to
  # print to a "Microsoft XPS Document Writer" which saves the printer output
  # to a file.  The file is subsequently read by an AutoHotkey macro, and the
  # contents are made available for paste.
  [[ "$TERM" == "xterm" ]] || return 0 # This prevents recursive runs, in case t() is called after tmux is started.
  { while :; do tput mc5; cat ~/.tmux-buffer; tput mc4; sleep 5; done } &
  tmux attach || tmux
}

3) On client-side (Microsoft Windows), create new printer:

  • Add a Printer
  • Create a new port > Local Port
  • Enter a port name > "PuTTY_Printer_File"
  • Driver > Microsoft XPS Document Writer
  • Printer name > "PuTTY Printer"
  • Optional: Print a test page and make sure it shows up in contents of file @ "%USERPROFILE%\Documents\PuTTY_Printer_File"

4) In client-side PuTTY configuration:

  • Set Terminal > "Printer to send ANSI printer output to:" to newly created printer named "PuTTY Printer"
  • Set Connection > SSH > "Remote command:" to "t" (referencing the .bashrc function above)

At this point you can send the contents of the tmux buffer to your PuTTY client by highlighting some text in tmux's copy-mode, and pressing y. The selected text will end in up %USERPROFILE%\Documents\PuTTY_Printer_File back on the client. If you want to go a step further and emulate "pasting" out of this file, you can use a hotkey sequence to read the contents of the file and insert it. Here's an example that leverages AutoHotKey, but it is probably possible to achieve the same result in PowerShell if you prefer.


5) Client-side AutoHotKey macro:

;### Get contents of PuTTY ANSI printer device output and paste it
#v:: ;Winkey + v
FileRead, PuTTYPrinter, %USERPROFILE%\Documents\PuTTY_Printer_File
SendInput %PuTTYPrinter%
PuTTYPrinter = ; Free up memory
return

6) Complete usage procedure:

  • Connect to server with PuTTY and get dropped into tmux by t() function.
  • When ready to select text for copy, use tmux hotkey for copy-mode ( Ctrl + b, [ )
  • Move cursor with arrow keys
  • Begin selection with spacebar
  • End selection and copy it with y
  • Back on client-side running PuTTY, WindowsKey + v will paste the selection

Since pictures are worth 1,000 words, here's an overview of what's happening: https://media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAfiAAAAJDYzM2RmMzYzLTk1NmQtNGQxMi1iN2YyLTQ4NGUxNjExMmVlOA.png

Solution 5

Assuming mouse mode is already on, add this to your tmux config and selecting will now copy to your clipboard.

bind -Tcopy-mode MouseDragEnd1Pane send -X copy-selection-and-cancel\; run-shell -b "tmux show-buffer | clip"

Tested on v2.6

Share:
26,967
Vivek Jha
Author by

Vivek Jha

Updated on March 29, 2020

Comments

  • Vivek Jha
    Vivek Jha about 4 years

    ENV: I am running tmux in putty, on a windows 7 laptop. I do ssh to linux systems while working.

    I have to solve two glitches with tmux, before using it for all my needs.

    1. I have to copy some text to the windows notepad from vim or the tmux terminal. I have enabled mouse support for both vim and putty.

      I have tried "*y and "+y but both of them doesn't work. If I use putty's ability for copy then it copies the content of adjacent grids also.

    2. I have to copy some content from vim file in one pane to vim file opened in another pane. Is there any vim trick for this. I don't want to use <C-b>[ way of doing it.

    Problem 1 is haunting me more, because there is a w/a for problem 2.

  • Alnilam
    Alnilam over 9 years
    This answer is wrong. Islab describes it best below. It is both possible and hassle free. The simple answer is to enable the mouse and hold down Shift and you can use your mouse to copy text into your Windows clipboard. I do this daily with Win7 and tmux running in Putty. I haven't tried the mouse to paste. For me I paste whatever text I have in the Windows clipboard into the putty terminal (usually into something I'm editing in Vim) using Shift + Insert.
  • Ingo Karkat
    Ingo Karkat over 9 years
    @FooBar: Yours works for the currently visible text, my answer is concerned with the general case of copying arbitrary amounts (i.e. also invisible) of text.
  • Alnilam
    Alnilam over 9 years
    @IngoKarkat: After carefully rereading the OP, I stand corrected and apologize. I came across this post because I needed to simply copy text out of a single pane in Tmux to something in Windows. The shift key and mode-mouse solves for that. The OP is complaining because this doesn't work for text in Vim in a multipane window (vertically split) because the mouse highlighting spans panes. A possible solution would be to use pane zooming and then shift-highlight the text. Line numbers have to be off in Vim or you yank those too. What you would consider as an example of "invisible" text, though?
  • Ingo Karkat
    Ingo Karkat over 9 years
    @FooBar: Invisible text is from the current buffer, but not displayed in the corresponding Vim windows, i.e. scrolled off the page. With Vim commands, you can easily grab that, too. With terminal means, you can't.
  • sparticvs
    sparticvs over 9 years
    You also need to have "Shift overrides application's use of mouse" checked in the PuTTY config under Window > Selection
  • lsowen
    lsowen about 7 years
    Per this post, it looks like the option changed from setw -g mode-mouse on to set -g mouse on in tmux 2.1+
  • jira
    jira over 6 years
    Hold Alt when selecting in Putty. This will enable block mode selection - i.e. you will be able to select text only within desired pane/vim window.
  • Alan Garrido
    Alan Garrido over 6 years
    This a more complicated solution but works like charm! Plus no need to hold the shift key: Shared buffer tmux-putty clipboard
  • Maxim Suslov
    Maxim Suslov over 5 years
    BTW, SHIFT+ALT+mouse does vertical/block selection.
  • Pushpendra
    Pushpendra almost 4 years
    I have tried all but things are not quite working for me. I face no problem if open file and doesn't do PageUp and PageDown. But after couple of PageUp PageDown copy/paste stops working. I'm using accessing Linux via Putty on Windows, in which Vim is running under Tmux window. And from this vim I'm copying to Notepad++ on windows by selecting text. In putty text get copied by just selecting. Note I didn't face this issue on Older windows version.
  • Mohammed Safwat
    Mohammed Safwat about 2 years
    Where should the AutoHotkey macro be stored?