Getclip and putclip

From EggeWiki

getclip and putclip are two tools used by power Cygwin users. For years I went about not knowing about these two very useful commands. Quite simply, they either copy the clipboard to standard out or copy stdin to the clipboard. For example, to copy a file onto the clipboard:

<geshi lang="bash"> putclip < /etc/hosts </geshi>

Or to write the clipboard to a file: <geshi lang="bash"> getclip > myfile.txt </geshi>

Another useful feature I've found is writing small scripts to transform whatever is on the clipboard. For example, I needed to copy column data from one application, and paste it into my Eclipse IDE. I would copy the data, run the following script, and then paste.

<geshi lang="bash">

  1. !/bin/bash

tmp=/tmp/$(basename $0).$$ lines=$(getclip | wc -l) if [ "$1" == "--quote" ]; then

 getclip | ruby -ne 'i = $_.strip; print "\"#{i}\", "' > $tmp

else

 getclip | ruby -ne 'i = $_.strip; print (if i =~ /^$/ then "" elsif i =~ /^\d+[.]0+$/ then "#{i.to_i.to_s}, " elsif i =~ /[A-Za-z]/ then "\"#{i}\", " else "#{i}, " end)' > $tmp

fi putclip < $tmp /bin/rm $tmp echo "Transformed $lines items" </geshi>

Another example, is running a diff over two clipboard items you've copied to the clipboard. <geshi lang="bash"> getclip > /tmp/a getclip > /tmp/b vi <(diff <(sed 's/></>\n</g' /tmp/a) <(sed 's/></>\n</g' /tmp/b) ) </geshi>

Edit the clipboard

Occasionally one needs to edit the content on the clipboard. For example, if a Windows dialog box does not give you much space to edit, you can copy the contents to the clipboard, edit it vim, and then paste it back. The following script helps you use Cygwin to do this:

<geshi lang="bash">

  1. !/bin/bash

getclip > /tmp/clip.$$ touch /tmp/clip.now vim /tmp/clip.$$ if [ /tmp/clip.$$ -nt /tmp/clip.now ]; then

 putclip < /tmp/clip.$$

fi

/bin/rm /tmp/clip.$$ /tmp/clip.now </geshi>

Note, on the Mac you can do the same thing with pbcopy and pbpaste