14

I want to copy some text from screen to my clipboard. I know how to get into copy and paste mode in screen, but I want that text to go to my clipboard so I can paste it on the browser, for example.

How do I do this? Thanks!

4 Answers 4

10

In most programs, you would select text and press Ctrl + C to copy it. Pasting text would be done by pressing Ctrl + V. In the terminal emulator program, Ctrl + C has a special meaning, it interrupts (stops) programs by default.

Your terminal emulator program may have other shortcuts defined. For example, Konsole and Gnome Terminal uses Ctrl + Shift + C for copying the selected text. Alternatively, select text and copy it by using the Copy option in the Edit menu or the context menu.

If the output of your program is large, you might want to enable the scrollback buffer of screen. That can be done by adding the below line to the ~/.screenrc file:

termcapinfo xterm ti@:te@
7
  • 2
    This won't work if the text you're trying to copy from GNU Screen exceeds the actual screen real-estate Aug 10, 2011 at 13:59
  • I usually scroll back then (or just continue scrolling while selecting).
    – Lekensteyn
    Aug 10, 2011 at 15:18
  • You mean while in Copying mode in screen? Aug 10, 2011 at 15:21
  • 3
    Nope, when selecting text in regular mode using the mouse. For that to work, you need to put termcapinfo xterm ti@:te@ in your ~/.screenrc file to allow scrollback. If you've no .screenrc, get my screenrc from lekensteyn.nl/files/screenrc
    – Lekensteyn
    Aug 10, 2011 at 16:11
  • Ah, that's it then, thank you for clarification. You may want to add that to your answer. Aug 10, 2011 at 16:28
10

For a raw and dirty implementation, install the package xsel, that allow command line interaction with then X clipboard, then put these lines in your ~/.screenrc

bind > eval writebuf "exec sh -c 'xsel -nbi </tmp/screen-exchange'"
bind y eval "exec sh -c 'xsel -bo >/tmp/screen-exchange'"

then restart screen.

Now, when you select something in screen copy/scrollback mode, and write paste buffer to a file with C-a >, you can move to the browser and paste the content of the clipboard as usual (for example with Ctrl-V).

For the reverse, copy something in your browser with Ctrl-C, then go to screen and read the clipboard with the new command C-a y, then read the screen-exchange file into the paste buffer with C-a <, finally paste wherever you want with C-a ].

For this to work the variable DISPLAY must be correctly set, and you must have access to the X server. This is not a problem when you are on a terminal running on the same X server, but if you run screen in a virtual terminal, e.g. tty1, then you may need to run xhost + on the X side, and export DISPLAY=:0 or similar on the terminal, before starting screen.

As you see, it is not simple, and not bullet proof, but I hope it can help.

This answer is partly inspired from synchronizing GNU screen’s paste buffer and the X selection

1
  • The xhost + is terrible advice. It allows anyone on the internet to hijack your X session, capture your keystrokes, take screenshots, etc. Never do it. There are correct ways to do the same thing; as long as you're on the same host and using same homedir, just setting $DISPLAY should work. Feb 14, 2015 at 18:08
0

Just use copy operation in your terminal application. If you are using gnome-terminal for example, just select test and use context menu -> copy.

1
  • 1
    What if the terminal application doesn't have a context menu? What if it doesn't have a copy operation? I didn't downvote cause I don't believe it deserves more than one.
    – djeikyb
    Aug 11, 2011 at 4:09
0

In Ubuntu 22.04, add this to ~/.screenrc:

bind "^K" screen bash -c "nohup >/dev/null 2>&1 xclip -selection clipboard /tmp/screen-exchange"

Note that this rebinds the default kill command bound to C-a C-k to something more idiomatic for Emacs users.

See also a similar answer to do the same thing in Tmux.

Then restart screen or source the file with C-a : source ~/.screenrc RET.

Now typing C-a C-k will copy the contents of /tmp/screen-exchange to the clipboard.

For example, from an open screen, you could use it like this:

  • Type C-a ESC Y to copy the current line into the paste buffer
  • Type C-a > to copy the paste buffer to /tmp/screen-exchange
  • Type C-a C-k to copy /tmp/screen-exchange to the clipboard

To wrap it all up into one command usable from copy mode, you can also add the following to ~/.screenrc:

bindkey -m "^K" eval "stuff '>'" "screen bash -c 'nohup >/dev/null 2>&1 xclip -selection clipboard /tmp/screen-exchange'"

Then the usage is even simpler:

  • Make a selection, e.g. type C-a ESC SPC w to select a word
  • Type C-k to copy it to the clipboard

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .