1

For Emacs 26.3 running in terminal mode, I am trying to map a my-command to M-Z in org-mode by using the following code:

(with-eval-after-load org
   (define-key org-mode-map (kbd "<M-Z>") #'my-command))

But this does not work.

It works if I do this instead:

(with-eval-after-load org
    (define-key esc-map "Z" #'my-command))

But the latter binds my-command in the global map making it available everywhere, and I do not want that.

This behaves as if the esc-map takes precedence over the org-mode-map. I looked in org.el and I don't see that code copying anything from the esc-map defined in subr.el.

I thought it could because the org mode function has not yet run, so I tried adding the code inside a org-mode-hook as well (in addition to the above code):


  (defun pel--org-set-keys ()
    "Set keys."
    (if (boundp 'org-mode-map)
        (progn
          (define-key org-mode-map (kbd "<M-Z>") #'my-command))
      (display-warning 'pel--org-set-keys
                       "org-mode-map not bound"
                       :error)))
  (add-hook 'org-mode-hook (function pel--org-set-keys))

But that still does not work.

Why does the (define-key org-mode-map (kbd "<M-Z>") #'my-command) not work?

1 Answer 1

2

You don't want (kbd "<M-Z>"). You want (kbd "M-Z").

kbd expects the same key-description (name) syntax as that used by Emacs itself in *Help*. If you use C-h k followed by Meta + Shift + z then Emacs tells you that this is key M-z, not key <M-Z>.

4
  • It's been one of those days... Thanks.
    – PRouleau
    Oct 20, 2021 at 22:24
  • ;-) It's easy to get confused with this stuff. Not to mention that the key description of M-Z is M-Z, but the key description of Ctrl + Shift + z is C-S-z, not C-Z!
    – Drew
    Oct 21, 2021 at 0:44
  • :-), I started off modifying something that was <M-right> and wanted to transform it into M-Z and forgot to remove the angle brackets. Staring at the code I did not see the nasty angle brackets and was digging into how the key maps were handled... I also was focusing on the esc-map becuase of what I was into. I know that stuff, I should have just been looking at what I wrote... Oh well...
    – PRouleau
    Oct 21, 2021 at 11:22
  • Thinking about it, I think I was focussing on what I might not know instead of what I knew... That's an example of why team work and code reviews are important!
    – PRouleau
    Oct 21, 2021 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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