Making Emacs Keybindings Work With Zsh and Screen

I’ve always (as in from the early OpenACS days) been an emacs guy on linux. Sure, I do most of my development on TextMate these days, but for all text editing on servers I still prefer emacs and have its keybindings tattooed deep in my brain:

Ctrl-a Beginning of line
Ctrl-e End of line
Ctrl-k Kill the rest of the line

Of course, these are the default bindings in bash as well.

However, I’m using GNU screen to stay sane while working over unreliable server connections. Screen uses Ctrl-a by default as its escape key so to make it work correctly, I’ve had this in my .bash_profile files for a long time:

alias scr="TERM=vt100 ; screen -e^Oo"
alias scd="TERM=vt100 ; screen -d -r"

That makes (among other things) Ctrl-o to be the escape key binding and lets Ctrl-a do the thing it’s supposed to do.

However, I’ve heard so much talk about zsh from many directions lately, that I had to try it out myself. With zsh (.zshrc downloaded from here) the keys worked fine without screen, but within a screen session they broke. Here’s what I had to do to make everything work.

First, setting the default escape key in the screen alias command didn’t work. I had to do it in the ~/.screenrc file instead:

escape ^Oo
startup_message off

(The second line just turns off the annoying splash screen)

However, with this and the default key bindings Ctrl-a and friends still didn’t work in screen, they were just output as literals on the screen. Some determined googling finally turned up the key (pun unintended) to the puzzle:

bindkey -e

Adding that one line into my .zshrc among the other keybindings solved the problem. Now I’m one happy zsh+screen camper, although my journey is still at a very early stage and I’m yet to learn the more advanced zsh goodies. The nice thing has been that without learning really anything new (or changing any habits), I’m still benefiting from using zsh.