87

I see here:

http://www.pgrs.net/2008/1/11/command-line-clipboard-access

that there's a way in linux and osx to copy to the clipboard from the command line. So I ran my cygwin setup.exe, but couldn't find the xsel package. I'm guessing maybe this package hasn't been ported to windows? Looks like there's a tool to do it in windows:

http://www.labnol.org/software/tutorials/copy-dos-command-line-output-clipboard-clip-exe/2506/

I guess I'll try that - but in the mean I figured I'd ask if anyone has found a good solution.

spender
  • 117,338
  • 33
  • 229
  • 351
andersonbd1
  • 5,266
  • 14
  • 44
  • 62

7 Answers7

177

Cygwin comes with special device file called /dev/clipboard:

echo foobar > /dev/clipboard  # Puts "foobar\n" on the clipboard
cat /dev/clipboard  # Pastes clipboard to stdout
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 3
    this works also, but I guess I can't accept 2 different answers – andersonbd1 Aug 26 '09 at 12:51
  • 5
    This is better than getclip/putclip, as it requires no package installation. – Stabledog May 04 '14 at 19:31
  • 8
    The advantage of getclip/putclip over /dev/clipboard is that the former have options to convert between unix and dos line endings. – esquifit Dec 28 '14 at 20:03
  • 8
    The advantage of /dev/clipboard is that it supports UTF8, whereas getclip/putclip doesn't. – Maximilian Hils Feb 29 '16 at 16:10
  • 3
    And it's easy to create `putclip` containing `cat - >/dev/clipboard` and `getclip` containing `cat /dev/clipboard` -- as aliases or functions or scripts, whichever takes your fancy. You can use any pair of script names, of course. Mac's have `pbcopy` and `pbpaste` to put and get information from the pasteboard (clipboard), for example. – Jonathan Leffler Nov 20 '17 at 20:31
  • /dev/clipboard is pretty gangster. – Nathan Chappell Nov 11 '21 at 09:46
67

On the page you linked, there are comments hinting how to do it on windows:

On Windows, Cygwin comes with getclip and putclip which do the same job.

Andre Miller
  • 15,255
  • 6
  • 55
  • 53
  • 17
    If you don't have these available, you'll need to install the `cygutils-extra` package ([src](https://cygwin.com/ml/cygwin/2013-06/msg00503.html)). Interestingly [Babun](https://babun.github.io/) omits this package by default. – chrnola Dec 03 '14 at 14:40
28

I second the answer above

To cat text to the Windows clipboard

putclip < foo.txt

To pipe to a file whatever text is in the Windows clipboard

getclip > foo.txt
  • @Adrian Thanks for noticing my useless use of cat. I tend to overuse cat instead of just pipes. I updated my answer. –  Feb 13 '13 at 23:34
25

getclip/putclip is found in cygutils-extra package.

yskkin
  • 854
  • 7
  • 24
13

what about just

clip < file.extension

just tried in on my ssh key

  • 1
    `clip.exe` was introduced in Windows Vista, so yes, you can use that as well, but the options are limited. – RobSiklos Mar 20 '18 at 15:39
2

Actually google "resource kit clip " for your windows clip and in cygwin terminal ( I use puttycyg works the following: find | clip

Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
2

Not exactly Ditto, but here's a clibboard logger.

#!/usr/bin/ksh
while true
do
    if [[ "$(</dev/clipboard)" = "${LastClip}" ]]
    then
            sleep 2
    else
            LastClip="$(</dev/clipboard)"
            echo "$(</dev/clipboard)" >> $HOME/cliplog.txt
            sleep 1
    fi
done