Integrating GNU Screen copy/scrollback mode with the local system clipboard
GNU Screen is a great UNIX tool, allowing a single terminal session to multiplex and manage multiple virtual terminal windows. As a long time user of screen, over 10 years, one of the features I frequently use is the copy/scrollback mode. Oftentimes, I wish this mode had better integration with the system clipboard.
Copy/Scrollback mode (CTRL-A ESC) in screen allows you navigate the buffer of screen's virtual terminals using vi-like keybindings (hjkl, etc). An additional feature of this mode is the ability to mark portions of text in the buffer to be copied into screen's internal clipboard. This internal clipboard is only usable for writing to a preconfigured filename (CTRL-A >) or using a screen command to paste (CTRL-A ]) into a virtual terminal.
I recently came across a blog article touting the benefits of using screen on Mac OSX. One of the sections described "Copying to the Mac Clipboard" using OSX's built-in pbcopy command to integrate screen's copy buffer with the Mac's own system clipboard. This inspired me to come up with a similar solution for use on Windows.
My solution is more complex than simply using a command line program as I often will start screen sessions on remote systems and would still like to maintain the integration with the local system clipboard. pbcopy can't do this. X11 users have it easy with the xclip program, using forwarded X11 sessions with ssh, it is trivial to integrate remote screen copy-buffer with the local clipboard. Windows doesn't have any answer to this problem at all.
First, I wrote a small Java program to listen on a TCP port (4573) and forwards all data received into the local system clipboard; the program is called Clipboard Listener and source is available under the GPL. The program can be minimized to the system tray or notification area to be as unobtrusive as possible. It displays a notification bubble whenever new data is received on port 4573.
Next, there is a small shell script (copy-to-clipboard) which invokes netcat (nc), commonly available on Linux systems and easily installed if not found. Replace CLIPBOARDLISTENERHOSTNAME with localhost (in ssh) or the host upon which the clipboard listener is running on.
#!/bin/sh
if [ -z "$1" ]; then
echo "Usage: copy-to-clipboard <file>"
exit 1
fi
nc CLIPBOARDLISTENERHOSTNAME 4573 < "$1"
Third, we setup screen to invoke the copytoclipboard script upon our screen selection. Add or edit the following lines into $HOME/.screenrc; replace /PATH/TO/ with the appropriate locations.
# Buffer file is where the screen copy buffer will be saved to.
bufferfile /PATH/TO/screen-copy-buffer
bind y eval "writebuf" "exec /PATH/TO/copy-to-clipboard /PATH/TO/screen-copy-buffer"
Alternatively, instead of using the copy-to-clipboard wrapper, nc can be invoked directly using the following bind. I use the former rather than the latter because I may attach to a certain screen session from more than a single host; I will edit copy-to-clipboard to point to the appropriate host, rather than change the screen keybinding.
bind y eval "writebuf" "exec sh -c 'nc CLIPBOARDLISTENERHOSTNAME 4573 < /PATH/TO/screen-copy-buffer'"
Basically with those steps done, we have completed our integration of screen and the system clipboard. Additional steps have to be taken when integrating remote sessions via ssh (adding the flag -R 4573:localhost:4573). With the integration complete, we can now use the y (named after vi's yank) binding to forward screen's copy buffer into the local system clipboard (CTRL-A y). You can now use your system's paste functionality (such as CTRL-V) into any application which supports it. The clipboard listener application is portable across platforms, you should be able to use this technique on OSX as well if you have a desire to integrate remote screen sessions.
Note: If you use an escape key other than CTRL-A for screen (I use CTRL-O), substitute CTRL-A with your choice of escape key.
8