Messing with the Clipboard

So you want to dabble in commandline wizardry, but you don’t want to get into that hard bash scripting stuff just yet? With these, you can live in the GUI and use a terminal to mangle your clipboard.
 
For the casual piping exercise we’ll sort this list:

Hermes
Zapp
Leela
Amy
Zoidberg
Fry
Bender
Professor
Kif

 
I’ve got instructions for each platform below the fold.
And for advanced folks, there’s a way to run a script without saving a text file.
 


OSX

Setup:
None
 
Casual piping:
1) Copy the above list from your browser
2) pbpaste | sort | pbcopy
3) Paste the newly sorted list in a text editor
 
Impromptu scripting:
1) Copy this from your browser
echo “hi”
2) pbpaste | bash
 


Linux

 
Setup:
sudo aptitude install xclip
 
Add this line to ~/.bashrc; then get a fresh terminal so it kicks in.

alias xclip=’xclip -selection clipboard’

 
Casual piping:
1) Copy the above list from your browser
2) xclip -o | sort | xclip
3) Paste the newly sorted list in a text editor
 
Impromptu scripting:
1) Copy this from your browser
echo “hi”
2) xclip -o | bash
 


Windows

 
Setup:
Get yer UnixUtils. (Strictly, all you need are gclip.exe & pclip.exe from UnxUtils.zip)
 
Casual piping:
1) Copy the above list from your browser
2) pclip | sort | gclip
3) Paste the newly sorted list in a text editor
 
Impromptu scripting:
1) Copy this from your browser
(I have a space instead of a true blank at the end here since browers highlight differently than real editors)

@echo off
REM cmd’s gonna insist on that Microsoft copyright banner…
cls
echo “hi”
echo “Make sure you copy a blank line after this or it’ll complain, ‘more?’”
 

2) pclip | cmd /Q
 


 
You can do way more than sorting of course:

  • filter lines (grep “keyword”)
  • replace words (sed “s/now/then/g”)
  • lowercace characters (tr “[A-Z]” “[a-z]“)
  • extract the 2nd column from a tab-separated table (cut -f2)
  • download a list of urls (wget -i -)
  • remove duplicate lines (sort | uniq)

If this is something you wind up doing a lot, you can put the pipeline into a script, which is double-clickable with no terminal at all.
 


 
Update (2009-11-28): Here’s a nifty way to independently filter a mixed list several times at once, on any of the platforms.

((pclip|grep K)&&(pclip|grep r)&&(pclip|grep -i a))|gclip

K> Kif
r> Hermes
r> Zoidberg
r> Fry
r> Bender
r> Professor
a> Zapp
a> Leela
a> Amy

That’ll custom sort the Futurama characters, but it’s of practical use when rearranging a directory listing by extension (like blocks of mp3s, avis, then jpgs).

Tags: , ,

Leave a Reply