Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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 (, iTerm, iTerm2).

share|improve this answer
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 – Amelio Vazquez-Reina Sep 7 '12 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. – Gilles Sep 7 '12 at 22:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.