2

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).

marked as duplicate by Gilles 'SO- stop being evil', 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? – Amelio Vazquez-Reina 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). – Amelio Vazquez-Reina Sep 7 '12 at 22:18
  • Ah, oops, yeah, sorry. – Gilles 'SO- stop being evil' 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 'SO- stop being evil' Sep 7 '12 at 22:22
  • 1
    The documentation isn't wrong, this bit is mentioned in a different section. – Gilles 'SO- stop being evil' Sep 7 '12 at 22:49
2

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.

  • 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 ? – Amelio Vazquez-Reina 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 'SO- stop being evil' Sep 7 '12 at 23:15
  • @Gilles, sorry for bringing back an old question, in my case, after C-q and my desired key combo (Alt+CMD+left, where Alt is my Meta), all I get is ^[C as the escape sequence. According to your answer above, ^[ is the escape character for the meta key Alt, but what about the characters for CMD and left keys? I am using Emacs inside iTerm2 in OS X 10.10.5. – MLister Dec 2 '15 at 21:19
  • @MLister There's no standard for Cmd. It's possible that Alt+Cmd+Left sends an Esc character to represent Alt, followed by an escape sequence starting with Esc. In this case, Emacs receives two Esc characters and so the C-q trick doesn't work. Almost always in this case, the escape sequence is the same that Cmd+Left sends, so see what that sends and bind that sequence with an extra \e at the beginning. – Gilles 'SO- stop being evil' Dec 2 '15 at 21:29
  • @Gilles, you are right about the issue with CMD key, as my searches also lead me the same conclusion. However, what still puzzles me is that I then try Ctrl+Alt+left with the C-q trick, where Alt is still my Meta, nothing is output to the buffer (no escape sequence displayed). I wonder if this is related to some sort of key mapping setting(s) in iTerm2 and possibly OS X more generally. Is there any way to find out? – MLister Dec 2 '15 at 21:36

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