Is there a command in GNU Screen to direct standard output to the copy buffer

I've been doing alot of work with files, it's great to copy and past the filenames I want into commands


ls | grep proc

$ ls | grep cal

// enter copy mode
$^a y 
// backward search for gen_chg
? cal
// start region
//goto to end of file by forward search php
// done 
// type command line
$ git rm ^p
// creates
$ git rm calendar.inc-gen.php

I'm aware there are easier ways to do this with a bash script, but often I find myself needed a specialized piece of information from the terminal screen and it would be great to just type

ls | grep cal > To_GNU_SREEN_COPY_:) or something

dennis's solution below works

you may need to set the bufferfile in your .screenrc bufferfile /tmp/screen-exchange


Here's one way:

echo -n cal* > /tmp/screen-exchange
screen -X readbuf    # or press Ctrl-a <
git rm ^a]           # type "git rm" then press Ctrl-a ]
  • it doesn't work for me, but I'll try experimenting around in that area, thanks – Fire Crow Jul 18 '09 at 4:40
  • It worked mostly as Dennis said. Only small issue is that the echo will include a newline which may or may not cause problems with composing the command line with the paste (^a]). echo -n took care of that. A neat trick I learned today :-) – njsf Jul 18 '09 at 4:52
  • @njsf: The -n is a good idea. I've edited my answer. Thanks. – Dennis Williamson Jul 18 '09 at 6:17
  • @Fire Crow: What's happening, specifically? – Dennis Williamson Jul 18 '09 at 6:18
  • @dennis : it's just not in the gnu screen copy buffer Ctrl-a ] pastes nothing or the copy I made before running these commands, I'm in gentoo linux with gnu screen 4.00.03, I'm guessing it's in the readbuf line, maybe pointing to the wrong file – Fire Crow Jul 23 '09 at 16:17

The gnu-screen Copy / Scrollback Buffer is located at register .

To send a string to the Copy / Scrollback buffer from a shell within the screen session you can use the register command like so:

screen -X register . 'Hello World'

However, if this command is running from a script, as is likely the case then you will need to specify the screen server name.

screen -S "1234.pts-1.localhost" -X register . 'Hello World'

This command will also pull the value from a shell variable without the need for echo:

x='Hello World'
screen -S "1234.pts-1.localhost" -X register . $x

Specifying the window is not required, however this is how you would do it.

y='Goodbye World'
screen -S "1234.pts-1.localhost" -p 2 -X register . $y
# Walk into the ocean.... fade to black....  roll credits

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.