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 Comments

  1. jorges
    1/3/2007 #

    THANKS!

  2. Dan
    5/7/2007 #

    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…

  3. will
    2/26/2008 #

    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 http://www.google.com} you dont really need the writing to clipboard. i need a way to make screen read clipboard. is it possible

  4. raf
    3/15/2008 #

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

  5. 8/11/2008 #

    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.

    Cheers

  6. Gunnar Thielebein
    12/4/2008 #

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

  7. Hugo Heden
    3/8/2009 #

    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 :-) )

  8. 8/31/2010 #

    Better idea for pasting (using emacs-y ^y):

    bind ^y exec .!. xsel -no

    Easy? (Once you figure out how screen works?)

Post a comment...

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>