Copying the GNU screen buffer to the Leopard clipboard

by Chris Pettitt

Quite a while back I wrote an article about how I use GNU screen’s scrollback buffer. In the article I detailed how to copy the scrollback buffer into the Mac clipboard, which made it easy to copy data to other Mac applications. Unfortunately, I recently moved to Leopard and discovered this no longer works. In this short article I will explain an updated procedure that works for Leopard.

The Problem

In Leopard, both pbcopy and pbpaste appear to crash when run from GNU screen. I did some basic investigation and found that pbcopy and pbpaste work by talking to a daemon called pboard, which is managed by launchd in Leopard. Any subprocess launched by GNU screen seems to be unable to talk to pboard, though pbcopy does work with a pboard instance launched from within screen. Unfortunately this defeats the purpose because the pboard instance launched in screen does not work with the OS.

I was not successful in uncovering the reason that pbcopy in screen can’t talk to the pboard process started by launchd. If anyone knows why this is the case I would be very, very happy to hear about it.

My Solution

I realized that if I could somehow get pbcopy running in screen to talk to the pboard running outside of screen then I could write to the Leopard clipboard from screen. The solution I came up with is to start a process outside of screen that listens on a UNIX domain socket. When it receives a message it copies it to the Leopard clipboard using pbcopy. Within screen I use a client that pushes screen’s /tmp/screen-exchange file over the UNIX domain socket.

Here are the scripts:

You will need to chmod 755 pboard_server pboard_client after downloading them. To use the new pboard client, add the following to your .screenrc:

bind b eval "writebuf" "exec ruby pboard_client"

If you used my previous tutorial, the line above should replace the old bind b ... line.

Now when you open a terminal (outside of screen), run pboard_server &. This will start the process that listens for requests over the socket. You only need to do this once when you login, and you could automate this using launchd or some shell scripting from your .bashrc.

You should now be able to start screen and copy to the clipboard!