control arrow keys in rxvt, tcsh, and emacs

I use rxvt as my terminal, tcsh as my interactive shell, and emacs as my editor. I love them all.

However, for a long time, I mostly used the default key bindings in tcsh and emacs, which are different for a few things. The most noticeable was moving forward and backward by whole words, instead of characters. I used Alt-B and Alt-F in tcsh, since those were the defaults. They work in emacs too, but I used Control-Left and Control-Right in emacs since I grew up on Windows. (Go ahead and laugh. :P)

Recently, I got sick of this and figured out how to get control and arrow keys working in tcsh and emacs -nw, inside rxvt. When rxvt sees Control-Left and Control-Right, it translates them to Meta-O, d and Meta-O, c before passing them to the running application.

So, I just added these lines to my .cshrc:

bindkey "^[Od" backward-word  \# control-left
bindkey "^[Oc" forward-word   \# control-right

They should work for any csh derivate, not just tcsh. I also added these lines to my .emacs:

; rxvt's keycodes for C-<left> and C-<right> are M-O d and M-O c,
(global-set-key [(meta O) (d)] 'backward-word)
(global-set-key [(meta O) (c)] 'forward-word)

and I added these to my my .inputrc for bash and other apps that use readline:

"\eOd": backward-word
"\eOc": forward-word

Now control and the arrow keys should skip words, like god intended, even inside your terminal! This is especially useful when I’m ssh’ed into remote, headless servers, and using my minimal .emacs for fast startup.

I made a halfhearted attempt to find xterm’s keycodes for Control-Left and Control-Right, but they eluded me. C-h k (ie describe-key) in emacs doesn’t even catch them at all!

P.S. Thanks to MIT’s rxvt Reference and this cygwin mailing list thread.

Leave a Reply

Your email address will not be published.