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

LinuxTerminalCopy PasteGnu ScreenGnome Terminal

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

There is a simpler and less manual way to do this. In your screen .rc file, add the following line:

bindkey -m ' ' eval 'stuff \040' 'writebuf' 'exec sh -c "/usr/bin/pbcopy < /tmp/screen-exchange"'

How to use the copy functionality:

  1. screen -c path/to/screen/config.rc
  2. Hit Ctrl+A then Esc to enter copy mode.
  3. Scroll up the text buffer and find the spot you want to leave your start marker for copying, then hit space.
  4. Scroll down and select the text you wish to copy. When you are done, hit space again.
  5. The text will now be in your clipboard.

EDIT: On Linux with no pbcopy but with clipit, you can use as below:

bindkey -m ' ' eval 'stuff \040' 'writebuf' 'exec sh -c "/bin/cat /tmp/screen-exchange | /bin/clipit"'

Solution 3 - Linux

This answer works for only a scenario where your end target is to paste the copied buffer contents immediately.

The simplest way to do this is by splitting your screen into two regions. You can do this by hitting CTRL+a then |'This is not an i. It is the PIPE sign on your keyboard'

Hit CTRL+a then TAB to switch to the second region, CTRL+a then c to create a new session in the second region.

If you want to copy from nano and paste in terminal, open up the file in nano on the left region, hit CTRL+a then ESC, scroll to the start point of your copy location and hit SPACE, select the text by scrolling to the end point and hit SPACE again to mark copy.

Now, all you have to do is hit CTRL+a then TAB to switch to the region on your right and hit CTRL+a then ].

Your text will be written out to the command line. Note that you can also check for hardcopy option if you want to write directly to file.

Solution 4 - Linux

This answer applies to OS X.

After copying the desired text into the GNU Screen paste buffer using copy mode, do the following:

  1. In any of your screen windows, type pbcopy <enter>.
  2. Then paste your text into the terminal using the GNU Screen paste command (Ctrl-a ] unless you've changed your escape key).
  3. If the text does not end in a newline, press <enter> to insert one.
  4. Finally, press Ctrl-d to cause pbcopy to push the text to the system clipboard.

Then you can paste the text elsewhere in OS X as usual using Command-v or an equivalent menu option.

Solution 5 - Linux

Since nobody seems to have directly answered the question:

Once you have copied the output you want into your buffer you need to

  1. Open a text editor with a new file i.e. vim somefile.txt
  2. Go into edit mode i.e. i in vim
  3. Press Ctrl + a then ] which will dump the contents of the buffer you just filled into the text editor

ta-da!

Solution 6 - Linux

Exit your ssh terminal session, if you are currently connected to a server.

If you are using XQuartz on Mac OS and xsel on the server. You should update the XQuartz pasteboard settings by selecting Preferences in the xQuartz application menu.

XQuartz settings:

enter image description here

ssh into the remote machine and try run:

xsel -p <<<"THIS IS A TEST".

Press cmd + v and "THIS IS A TEST" should be output.

Solution 7 - Linux

I wanted a way to do this programmatically similarly to @kungfuspider and tweaked their solution to work for Ubuntu WSL running on Windows.

Setup:

  1. Download win32yank executable and place win32yank.exe somewhere useful (I created a symbolic link to it in /usr/bin with ln -s <path to exe> /usr/bin/win32yank)
  2. Place the following into ~/.screenrc (from @kungfuspider). You might need to modify the command to point to the correct cat and win32yank locations.

> bindkey -m ' ' eval 'stuff \040' 'writebuf' 'exec sh -c "cat /tmp/screen-exchange | win32yank -i --crlf"'

  1. Reload ~/.screenrc without killing your session by executing CTRL+a : source ~/.screenrc

How To Use:

  1. Enter copy mode with CTRL+a [
  2. Move around with vim style key movement or arrow keys
  3. Start selecting text by hitting space
  4. Highlight desired text and finish copy by hitting space again, text should now be in your Windows clipboard and can be pasted back to Ubuntu with a right-click.

Note: It is very important to finish copy with a space because that's what the bindkey command is using to map win32yank

Solution 8 - Linux

If it's just a little bit of info that you want to copy just highlight it with your mouse and then paste it where you want.

If you're trying to get a lot of info the screen session can be logged to a file and then you can copy from the file or clean it up a bit and use it for instructions on doing things

Solution 9 - Linux

Finally today I found a solution with mouse:

Hold down Ctrl and right click with mouse. Copy/paste context menu shows up.

Some screens at https://michalzuber.wordpress.com/2015/01/28/gnu-screen-copy-paste-with-mouse/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamuel G. P.View Question on Stackoverflow
Solution 1 - LinuxbonsaivikingView Answer on Stackoverflow
Solution 2 - LinuxkungfuspiderView Answer on Stackoverflow
Solution 3 - LinuxCoderXView Answer on Stackoverflow
Solution 4 - LinuxscottgwaldView Answer on Stackoverflow
Solution 5 - LinuxCpILLView Answer on Stackoverflow
Solution 6 - LinuxwattryView Answer on Stackoverflow
Solution 7 - LinuxwerepancakeView Answer on Stackoverflow
Solution 8 - LinuxJohnView Answer on Stackoverflow
Solution 9 - LinuxmichalzuberView Answer on Stackoverflow