i-think Twenty-Two

Now with more coherency.

Paste on the console

| Comments

I’ve already alluded to the fact that I love working on the console and in Windows, PowerShell is my console of choice. I get to throw all sort of things through it’s object based pipeline and play with them in fun and exciting ways.

I also have added to my path a large number of the GnuWin32 tools which are sometimes better at dealing with raw text like sed and occasionally grep instead of PowerShell’s Select-String. By having these utilities in my path I can better deal with being stuck in pure cmd.exe.

Anyway, one of my favourite utilities is Clip.exe. It comes out of the box in Windows 7 and presumably Windows Vista. I rolled my own for Windows XP although I believe that it may be available as part of a resource kit. In case it isn’t immediately obvious, what Clip.exe does is takes whatever is fed to Standard Input and saves it to the clipboard. Very useful indeed.

The clipboard is a great place for storing some data temporarily and I quite frequently find that I want to process it in various ways. I have a bunch of command line utilities that are perfect at this, but I have to save the contents to a file and pass that file to the utility. That sounds like busy work. Fortunately the GnuWin32 utilities and PowerShell both read from standard input. What I need is a tool that does the opposite of Clip.exe and writes the contents of the clipboard to standard input. What I need is Paste.exe. So I wrote it.

A simple compile…

$ csc .\Paste.cs

And after copying to my Utilities folder (which is in my path) I’m good to go.

An added bonus is that I handle files copied in Windows Explorer. These will be returned as a list of filenames.

By default I add an extra Environment.NewLine and the end of the content as it tends to make the whole thing neater in the console. If this is causing you hassles use the -r switch which will paste the contents in their raw form.

Paste in Action

I use paste to quickly view the clipboard contents:

$ paste

To strip formatting in the clipboard:

$ paste | clip

To filter clipboard contents:

$ paste | grep -i batman

As subjects of a PowerShell ForEach-Object:

$ paste | % { $_.Length }

If you find a common pattern, share it in the comments below.

Comments