How to copy from tmux running in putty to windows clipboard

前端 未结 5 580
难免孤独
难免孤独 2021-01-30 11:16

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.

5条回答
  •  借酒劲吻你
    2021-01-30 12:07

    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

提交回复
热议问题