Take the 2-minute tour ×
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.

Possible Duplicate:
Associating a function in Emacs after looking up the code with M-x describe-key

I have typed the following macros on my .emacs file

(global-set-key (kbd "M-<left>") 'windmove-left)          ; move to left windnow
(global-set-key (kbd "M-<right>") 'windmove-right)        ; move to right window
(global-set-key (kbd "M-<up>") 'windmove-up)              ; move to upper window
(global-set-key (kbd "M-<down>") 'windmove-down)          ; move to lower window

However, when I press Alt+left or any of the other keyboard combinations, I get errors like the following in the minibuffer M-o 3 c is undefined.

Why doesn't it work? I thought I was following the syntax described here (kbd macro).

share|improve this question

marked as duplicate by Gilles, Renan, jasonwryan, Tim Kennedy, Stéphane Gimenez Sep 30 '12 at 17:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Thanks @Gilles. This question is actually inspired by your answer to the other question. The kbd documentation I point to (and the same link you included in your answer) shows specifically an example with (kbd "C-M-<down>") . If so, why do I have to drop the angle brackets? –  user815423426 Sep 7 '12 at 22:16
    
@Gilles: The documentation specifically says: "you must surround function key names with <...>", and gives <down> as an example for the input to kbd (bottom of the page). –  user815423426 Sep 7 '12 at 22:18
    
Ah, oops, yeah, sorry. –  Gilles Sep 7 '12 at 22:19
1  
Your problem is the same as this question, so you need to declare that "\eO3c" is M-left in input-decode-map and so on. Is that close enough for a duplicate, or do you need a more precise answer? –  Gilles Sep 7 '12 at 22:22
1  
The documentation isn't wrong, this bit is mentioned in a different section. –  Gilles Sep 7 '12 at 22:49

1 Answer 1

up vote 2 down vote accepted

Terminals know characters, not keys, so keys with no corresponding character need to be translated into escape sequences and back. When you press Alt+Left, it is translated into the sequence of characters \eO3d where \e is an escape character. Unfortunately, some of these character sequences are not standardized, so you need to declare what your terminal sends to Emacs.

Press Ctrl+Q then the key combination (e.g. Alt+Left) to enter the key sequence in a buffer. The Ctrl+Q command tells Emacs to interpret the leading escape character literally. You'll see something like ^[O3d where that ^[ is not the two characters ^[ but an escape character, better written in an Emacs Lisp string as \e.

Add each key combination's escape sequence to input-decode-map. For example (this code goes into your .emacs):

(define-key input-decode-map "\eO3a" [M-up])
(define-key input-decode-map "\eO3b" [M-down])
(define-key input-decode-map "\eO3c" [M-right])
(define-key input-decode-map "\eO3d" [M-left])

(kbd "M-<up>") or (kbd "<M-up>") are alternative ways of writing [M-up].

Once you've declared these translations, Emacs will know that when the terminal sends e.g. ^[O3d it stands for the key combination Alt+Left, and your bindings will work.

share|improve this answer
    
Thanks! Somehow typing Ctrl+Q is not working for me, i.e. I don't get the escape sequence. Is there another way of invoking the function called by Ctrl+Q ? –  user815423426 Sep 7 '12 at 23:04
1  
@roseck M-x quoted-insert. If C-q and C-s aren't working, see gnu.org/software/emacs/manual/html_node/elisp/Input-Modes.html –  Gilles Sep 7 '12 at 23:15

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