How to copy the GNU Screen copy buffer to the clipboard?
Solution 1
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:
- Copy text from your screen session into GNU screen's copy buffer.
- Run this command within screen:
cat | xsel -b
- If
xsel
didn't report any error, now dump screen's copy buffer to STDIN:Ctrl+a+]
- 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
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:
screen -c path/to/screen/config.rc
- Hit Ctrl+A then Esc to enter copy mode.
- Scroll up the text buffer and find the spot you want to leave your start marker for copying, then hit space.
- Scroll down and select the text you wish to copy. When you are done, hit space again.
- 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
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
This answer applies to OS X.
After copying the desired text into the GNU Screen paste buffer using copy mode, do the following:
- In any of your screen windows, type
pbcopy <enter>
. - Then paste your text into the terminal using the GNU Screen paste command (
Ctrl-a ]
unless you've changed your escape key). - If the text does not end in a newline, press
<enter>
to insert one. - Finally, press
Ctrl-d
to causepbcopy
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
Since nobody seems to have directly answered the question:
Once you have copied the output you want into your buffer you need to
- Open a text editor with a new file i.e.
vim somefile.txt
- Go into edit mode i.e.
i
in vim - Press
Ctrl + a
then]
which will dump the contents of the buffer you just filled into the text editor
ta-da!
Samuel G. P.
I'm a web developer with knowledge in html, css, javascript, ruby, rails, php, zend framework, drupal, linux, apache, nginx, mysql and postgresql. And i love, really love, rails ;D
Updated on June 05, 2021Comments
-
Samuel G. P. almost 3 years
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 pressingspace
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.
-
Samuel G. P. about 11 yearsi'm trying to avoid the mouse use to get more speed in my workflow, and generally it's not too much info. thanks for the answer!
-
John about 11 yearsok. The copy/paste buffer in screen is only between screen windows. So you could have another window and do the copy paste to the other window or paste it out to a file.
-
yekta almost 11 yearsBeware, if you don't know what your doing with this (like myself)! The
cat | xsel -b
executed my entire history when I ran it from the shell. -
bonsaiviking almost 11 years@yekta there's no reason why it should do that. At what point in the 4 steps above did your shell start executing things? Did you somehow kill the
cat
command (with^C
, perhaps) so it was not accepting input when you dumped the copy buffer? If we can solve this, maybe you can remove your downvote. -
yekta almost 11 yearsExcuse me, I just reproduced it and I believe that happened when I executed
Ctrl+a+]
. I pretty much followed steps 1-3. Also, when I ran step #2 I getxsel: Can't open display: (null)
-
bonsaiviking almost 11 years@yekta Yes, in order to use xsel, you need to have an X display with a clipboard, and the
DISPLAY
environment variable set to it. Most terminal emulators will set this, as willssh -X
. Since it was not set, xsel fails before accepting input, so the pipeline quits and the rest of the "keystrokes" from screen's buffer go to your shell. -
yekta almost 11 yearsAh, that makes sense. If you'd like to edit the answer I can remove the down-vote. Having this information in the answer would also be helpful. Thanks.
-
Daniel Wagner almost 9 yearsThe
cat
incat | xsel -b
is a cute way to connectxsel
's input to a pipe (as opposed to atty
) and triggerxsel
's "input" mode; butxsel
has the-i
flag to explicitly trigger input mode. Soxsel -bi
is probably preferable tocat | xsel -b
. -
axd almost 8 yearsI think your solution is not a screen feature, but something related to X. what you need are the copy-paste tools explained here: gnu.org/software/screen/manual/screen.html#Paste
-
kungfuspider over 7 yearsI found a less manual way of doing this. Please refer to my answer below.
-
Nate Ritter almost 7 yearsIs there a similar method for this in Debian? Love the simplicity.
-
Benubird over 6 yearsThis is a description of how to use copy mode. The OP already knows how to use copy mode: the question is how to move data from the copy mode buffer to the system clipboard. This is not an answer to the question asked.
-
mivk over 6 yearsLogging the whole session to a file seems like the easiest and best way for certain situations. It would be helpful to know how to do that.
-
Mithun B over 6 yearsI am using putty from windows and login to Linux using screen. But don't have pbcopy on Linux I am using. What can I do here to copy text into my clipboad?
-
Strudle about 6 yearsthe same can be done in debian by using clipit. sh -c "/tmp/screen-exchange | clipit"
-
CoderX over 5 years@Benubird - I know. But I gave him another option to get where I thought he needed to go which is a copy paste. Helped someone!
-
Olian04 about 4 yearsOn ubuntu 18.04.4 I had to change
/bid/clipit
to/usr/bin/cliptit
and i had to installlibcanberra-gtk-module
. -
Ray almost 4 yearsThis is amazing! How did you figure it out?
-
CpILL almost 4 years@Ray skillz bro
-
Mark Reed over 2 yearsThe MacOS equivalent of this use of
xsel
ispbcopy
, notpbpaste
, which goes the other way (dumps the clipboard to stdout). -
Alex Amato over 2 yearsThis wasn't obvious to me when I first read it. But the big difference here is that this solution will copy the text to your clipboard, not just screen's copy buffer. Allowing you to paste in any application, not just ones running inside your screen session/terminal