How to copy the GNU Screen copy buffer to the clipboard?

Samuel G. P. picture Samuel G. P. · Apr 19, 2013 · Viewed 49.1k times · Source

When using GNU Screen we can work with scrollback buffer also known as "copy mode" using the Ctrl+a+[ command.

In there we can copy text to the copy buffer by pressing space selecting the text and pressing space again.

Is there some way to copy this text from screen copy buffer to the X clipboard?

In my case I'm using Ubuntu 12.04 with gnome and Xorg.

Answer

bonsaiviking picture bonsaiviking · Apr 29, 2013

You can use a CLI clipboard tool like xsel or pbpaste and the cat utility to grab contents from STDIN. The steps on Linux with xsel are as follows:

  1. Copy text from your screen session into GNU screen's copy buffer.
  2. Run this command within screen: cat | xsel -b
  3. If xsel didn't report any error, now dump screen's copy buffer to STDIN: Ctrl+a+]
  4. Send an EOF to cat to terminate it: Ctrl+d

At this point, the contents of the screen copy buffer should be in your clipboard.

EDIT: As with all X programs, xsel needs to know how to contact your X server in order to access the clipboard. You should have your DISPLAY environment variable set appropriately.