Is it possible to use C-up and C-down when running emacs in the terminal window (when starting emacs from the command line with the -nw flag)? What should my terminal send when I press C-up and C-down for these keys to work in emacs?

I'm on Mac OSX, using iterm2 to ssh into a remote linux server, where I start the emacs session from bash.

1 Answer 1


Terminals know characters, not keys, so keys with no corresponding character need to be translated into escape sequences and back.

You can pick any control sequences that doesn't cause any conflict and that iterm2 and Emacs agree on. Unfortunately, these control sequences aren't standardized. \e[1;5A and \e[1;5B are popular choices (where \e is an escape character).

Emacs has a fairly complex system to translate escape sequences into keys. See translation keymaps in the manual. Use input-decode-map unless you have an old version of Emacs that doesn't have it.

(define-key input-decode-map "\e[1;5A" [C-up])
(define-key input-decode-map "\e[1;5B" [C-down])

In iterm2, set the corresponding key sequences in the “Keys” tab in your profile. See How can I get control+left arrow to go back one word in iTerm2? for an illustrated guide.

See also Emacs + paredit under terminal (Terminal.app, iTerm, iTerm2).

  • Thanks Gilles. Do you know if there is a way to look up those control sequences for a given sequence of keyboard strokes? For example, I would like to look up Control + Shift + j to pass it to kbd later in my .emacs file Sep 7, 2012 at 22:50
  • @roseck I've answered your related question. C-S-j is probably send by the terminal in exactly the same way as C-j, though; how to change that is heavily dependent on your terminal emulator. Sep 7, 2012 at 22:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .