Emacs keybindings in GNU screen’s copy-scrollback mode

When I’m working, I pretty much live inside Emacs. It’s my IDE, debugger, text editor, email composer, spreadsheet, file manager, and all-around scratch pad.

I’ve burned Emacs’ key bindings into my brain, so I go to great lengths to set them in every program I use. That way, I don’t have to context switch my fingers’ muscle memory when I task switch from coding to email, to IM, to the web. Task switches may be a fact of life, but other unnecessary mental context switches are evil incarnate.

I recently started using GNU screen‘s copy/scrollback mode so that I could copy from terminals with the keyboard. I use these commands in my .screenrc to set up Emacs key bindings in screen’s copy/scrollback mode:

# emacs keybindings for navigation in copy mode
markkeys ^B=Y:^F=V:h=^B:l=^F:0=^A:$=^E

# C-space sets mark. markkeys does it in screen 4.0 and higher, but 3.9x
# needs the bindkey command. (note the ^@ escape sequence for C-space.)
markkeys ' '=^@
# bindkey -m ^@ stuff ' '

# page up and page down
bindkey -m -k kP stuff Y
bindkey -m -k kN stuff V

# failed attempt to make C-space work in i-search too
# bindkey -m ^@ eval "stuff \033" "stuff ^@" 

# special hack for C-e, since it should go *past*
# the last char. -m means this is for copy mode only.
bindkey -m ^e stuff "$^f"

# C-g and other keys just quit copy mode. Esc does nothing.
markkeys \033=\015=^G=^D=h=j=k=l=H=M=L=G=g=y=c=v=a=x=b=e=B=E=w
markkeys @=\033

# control arrows move by words. (set B, e, and w to F keys so that i can
# use them to move by words, but they themselves still quit copy mode.)
markkeys B=[:E=]:b={:e=}:w=>
bindkey -m ^[Od stuff {  #"[[}"
bindkey -m ^[Oc stuff ]  #"}]^f"

9 thoughts on “Emacs keybindings in GNU screen’s copy-scrollback mode

  1. The thing that drives me nuts about screen still (even though I love it and use it daily) is when I search throught the scrollback with C-r, once I found the spot I want, I can’t get out of incremental search unless I hit C-g, and then pops me back down to the bottom of the buffer! Do  you know how to prevent that?

  2. Hmmm, it seems ESC might do what I want. Don’t know why I hadn’t noticed that before.

  3. Were you able to move screen’s C-a to some other keybinding?  I use C-a to move to the beginning of a line, but screen wants to use that for its commands.  How can I tell screen to pay attention to C-s, or anything else?

  4. These two lines of your sample give me a syntax-error:

    markkeys 033=015=^G=^D=h=j=k=l=H=M=L=G=g=y=c=v=a=x=b=e=B=E=w
    markkeys @=033

    I use the following screenrc, it covers emacs-like window-management:

    escape ^x^x
    bind o focus
    bind 0 remove
    bind 1 only
    bind 2 split
    bind = resize =
    bind ^b windowlist -m

    One thing I miss is that C-X b RET should switch between two sessions. Any ideas?

  5. ack, the templating system ate the escape characters. fixed. thanks for the nudge.

    as for C-x b RET, off the top of my head, no, but i do know that bindkey can do amazing things.

  6. Sorry… I posted this comment on the wrong article!

    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…

  7. Since I use C-a all the time in emacs, I’ve mapped screen’s control key to ` (same key as tilde, but no shift press needed), using:

    escape “

    I also have jumping to start and end of buffer during scrollback with:

    M-< and M-> for start/end of buffer 

    bindkey -m “^[<” stuff “g” 

    bindkey -m “^[>” stuff “G”

Leave a Reply

Your email address will not be published.