5

I am trying to override Meta + left / right arrow keys in my emacs config and cannot figure out how to refer to the key sequence.

If I interact with Emacs directly I can type "M-x, global-set-key, M-, next-buffer", and it works fine. But I can't figure out how to type this into my init.el file. These are some things that I have tried:

(global-set-key "\M right" 'next-buffer)
(global-set-key "\M <right>" 'next-buffer)
(global-set-key [\M right] 'next-buffer)
(global-set-key [M right] 'next-buffer)
(global-set-key [M-right] 'next-buffer)
(global-set-key (kbd M-<right>) 'next-buffer)
(global-set-key [M (kbd <right>)] 'next-buffer)

etc.

More Info:

OK, this does work natively: (global-set-key [M-right] 'next-buffer) (thank you) - it's not working on iTerm2 in a VM (minor detail :) And for that environment: M-x describe-key does not open help but in *Messages* prints: ESC <right> (translated from ESC M-[ C) is undefined

And that's why I was confused and was not able to just paste that into kbd. And that's why I don't think it is being trumped by another mode.

ray
  • 61
  • 6
  • 1
    `(global-set-key [M-right] 'next-buffer)` works for me, so you probably have a major-mode or minor-mode that is trumping (taking precedence) over the global setting. Type `M-x describe-key` and then `M-right` and update your question or post a comment with the result. Also, provide what major-mode you are running and minor-modes that are active. You can type `M-x describe-mode` to find out all of that information. – lawlist Jul 18 '16 at 18:15
  • Re: edit, why do you say you were not able to use either `(kbd "ESC ")` or `(kbd "ESC M-[ C")` ? – phils Jul 19 '16 at 02:03
  • "why do you say..." sounds like `M-x doctor` – ray Jul 19 '16 at 18:04

2 Answers2

6

The easiest way to specify a key binding is always to use kbd.

(global-set-key (kbd "<M-right>") 'next-buffer)

kbd takes as argument an external key description, i.e., what Emacs tells you when you use C-h k.

Use C-h k, press and hold the Meta (e.g. Alt) key, and hit the right arrow key. Buffer *Help* tells you that this key sequence is written "<M-right>". So that's what you pass to kbd.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • That is not the case in my emacs 24.3.1. When I do the above, for metakey + up arrow, the help buffer says I pressed ``, but the global-set-key does not work with anything else other than `[s-up]`. – Sonny Mar 14 '17 at 18:22
  • @Sonny: Are you saying that you cannot do `(global-set-key (kbd "") 'next-buffer)`? (And do you mean `S-up` instead of `s-up`?) And it seems odd that your "metakey" acts as a super key (or as a Shift key). Is that what you see when you start Emacs using `emacs -Q` (no init file)? – Drew Mar 14 '17 at 21:34
  • yes. I tried `emacs -Q` and same thing. I meant `s-up`, BTW. I use emacs 24.3.1 on Ubuntu 14.04 on x86_64, though I am not sure if that is pertinent. – Sonny Mar 14 '17 at 22:21
  • OK. Sounds like a bug that `[s-up]` works and help tells you the binding is `` but the latter doesn't work (with `kbd`). You might want to do `M-x report-emacs-bug` - dunno. – Drew Mar 14 '17 at 23:22
0

Solved: (global-set-key (kbd "ESC <right>") 'next-buffer)

Thanks - I needed the combination of kbd and what to pass it.

ray
  • 61
  • 6