How to copy from tmux running in putty to windows

2020-05-13 03:52发布

问题:

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.

回答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.



回答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.



回答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).



回答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



回答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



标签: vim tmux