Olly Legg home

Clipboard Helpers for IRB

10th Feb 2013

I'm always looking for little ways to speed up my development. I'm a big fan of Mac OS X's pbcopy & pbpaste commands. I've quite often found myself wanting to use them in irb. Today I knocked up a simple addition to your .irbrc to access the commands from ruby.

module Clipboard
  def pbcopy(data)
    IO.popen('pbcopy', 'w') {|io| io.write(data)}

  def pbpaste
    IO.popen('pbpaste', 'r').read

include Clipboard

You can use it as follows:

>> pbcopy "Test"
=> 4
>> pbpaste
=> "Test"

I've added the code to a Gist. Use it, fork it, modify it.