synchronizing GNU screen’s paste buffer and the X selection

I hate the mouse. It’s good for some things, but not my work. Switching between the mouse and the keyboard wreaks havoc on my muscle memory and fine-motor flow. Thanks to Emacs, Ion, Pine, Gaim, and Firefox (God bless Find As You Type), I can do everything I need to with the keyboard. The only thing I can’t do with the keyboard is copy text from a terminal. And that irks me.

In the past, this has driven me to desperate measures, including running shells inside Emacs. I currently use GNU screen‘s copy/scrollback mode, with a hack to integrate it with X Windows’ clipboard.

This command in my .screenrc copies screen’s paste buffer into the X primary selection. It runs automatically when you press > to set the second mark in copy/scrollback mode.

# set the second mark, write to the screen-exchange
# file, and use xsel to synchronize the paste buffer
# with the X selection.
bindkey -m > eval "stuff ' '" writebuf "exec sh -c 'xsel -n < /tmp/screen-exchange'"

Note that this uses Conrad Parker’s xsel (not Ville Herva’s xsel), which needs to be somewhere in your PATH.

8 thoughts on “synchronizing GNU screen’s paste buffer and the X selection

  1. That is nice, but I don’t think you go far enough… What about mapping C-a to C-x (making commands much more Emacs finger friendly) … For example, I am now totally stuck on C-a p for ‘switching buffers’ after C-a c in screen. For this to be ‘Emacs’ it should be C-x b…

    Removing the C-a would be a start…

    Thanks for the others though… but where is C-f / C-b ? did you already define that in your rvxt ? (for me it just kills copy mode) .

    I guess I am looking for a very Emacs like shell, without running shell in Emacs…

  2. hrmm. im very willing to try this but what i really need is a way to have screen read from the clipboard. i need the file it uses if any. its fairly easy to do in screen irssi/other app via /exec appname command ie. in irssi {/exec firefox} you dont really need the writing to clipboard. i need a way to make screen read clipboard. is it possible

  3. what about that?
    bindkey < eval “stuff ‘ ‘” “exec sh -c ‘xsel -o > /tmp/screen-exchange'” readbuf

  4. If you would like full featured keyboard control over firefox user vimperator extension for firefox. I know it is vim-like extension, but is quite powerful.


  5. this .screen config borks up vim completly but as you are emacs guy don’t think this will bother you ;-)

  6. Excellent trick, thank you!

    How come you run xsel with the -n (–nodetach) option? I see you added a “stuff ‘^C'” as well in your screenrc in order to kill the xsel process.

    What do you gain by doing this?

    It does not work well for me… The ^C does not seem to always do its job properly.. So I just run xsel without -n

    bindkey -m ^[w eval “stuff ‘ ‘” writebuf “exec sh -c ‘xsel < /tmp/screen-exchange'”

    (Using emacs style M-w for copy :-) )

Leave a Reply

Your email address will not be published. Required fields are marked *