PuTTY SSH / Windows Clipboard Integration

Getting text out of your SSH sessions can be annoying.  If it fits in a screen, I usually just select the text in PuTTY to copy it– no big deal.  Otherwise, I have to either email it to myself or put it in a file, log in with WinSCP, and download the file.  Way too much trouble.

Today I started searching for a better solution and found a patch for PuTTY by Diomidis Spinellis.  Unfortunately it was for an old version of PuTTY.  However with a little effort I updated the patch to PuTTY 0.60.

Here’s how it works:

  1. Use the modified PuTTY
  2. When connecting (or after) click on the Terminal setting, and under “Printer to send ANSI printer output to” select “Windows clipboard”
  3. Add the following to your .bashrc (or the appropriate startup file for your shell)  To use it immediately, be sure to type “source .bashrc”
    function wcl {
      echo -ne '\e''[5i'
      cat $*
      echo -ne '\e''[4i'
      echo "Copied to Windows clipboard" 1>&2
    }
    

    }

  4. Now when you want some text out of SSH, just pipe it to wcl.   For instance, “ls -la |wcl” places the output of “ls -la” in your Windows clipboard.

Modified PuTTY 0.60 executable

Patch for PuTTY 0.60

This entry was posted in sysadmin. Bookmark the permalink.

14 Responses to PuTTY SSH / Windows Clipboard Integration

  1. Pingback: Print to Clipboard in Putty - ootput burst!

  2. puttycyg user says:

    That’s great that you have a way to integrate putty with the clipboard. Sometimes you can’t select all the text you want to copy with the mouse, and it’s nice to be able to just pipe that to the clipboard.
    How can this be integrated with puttycyg?

  3. eric says:

    puttycyg user, have you tried applying the patch to puttycyg and compiling it from source? That may work.

    However, on Windows this shouldn’t be necessary since you can pipe your command output to “clip” and it will be on your clipboard. i.e. “dir |clip” in Windows command prompt or “ls -la | clip” in Cygwin. I don’t have Cygwin installed but I just tried it in MSYS and it worked fine.

  4. joey says:

    This patch should be included into PuTTY upstream!

    I don’t see anything like it on the wishlist…
    http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/

    It looks like you can just attach the patch to a polite note sent to the address at the bottom of this page:
    http://www.chiark.greenend.org.uk/~sgtatham/putty/feedback.html

    I’d do it myself but its your patch (somewhat).

  5. Bryan Ruiz says:

    This is great! Just what i was looking for. It’s a pain because I’m using a patched putty already, so I’ll have to get visual studio to compile this thing with my version. I wonder if its possible to just have an actual printer installed on your system called “clipboard” so putty patching wasnt necessary.

  6. eric says:

    Glad it helped. You can also build it with MinGW and msys, that’s what I did.

  7. Leandro Stasi says:

    It was really easy to setup and it almost work perfeclty, but I only get last part of the text in the clip board, it gets choped at around 1000 chars.

  8. eric says:

    Are you using Screen? I have observed the same behavior under Screen and never did find a solution.

  9. boris says:

    Hi,
    thanks for the modified 0.6 version.
    Unfortunately, the option “Windows clipboard” is not saved to the registry when saving a setting, the key is left empty. Even changing the “Printer” key to “Windows clipboard” with regedit does not help, I always have to change it manually with the putty frontend.
    What happens? What’s the correct registry “Printer” key value?

    Regards,
    Boris

  10. eric says:

    @Boris Good question. I remember noticing that as well. I wasn’t able to find a quick solution from looking at the code. I personally stopped using this when I found out it doesn’t work well with GNU Screen, so I haven’t looked into it any further.

  11. clipboard says:

    Maybe a solution 🙂
    From the putty title bar right click
    Clear Scrollback
    cat /var/log/syslog
    Copy All to Clipboard
    Press Strg+V in your Windows Application
    Voila there it is.

    hf,

    Joerg

  12. Jeremy says:

    Here is a version that works for me with ksh.

    function wcl {
    echo '33''[5i'
    cat $*
    echo '33''[4i'
    echo "Copied to Windows clipboard" 1>&2
    }

  13. Jeremy says:

    …and the ’33’ should really be prefixed with ‘\’ then ‘0’ but the cut and paste did not save correctly. slash, zero, 3, 3 being the octal escape character.

  14. Matheus says:

    Has anyone found out how to set a default Printer on Putty?

Leave a Reply

Your email address will not be published. Required fields are marked *