3

I have this for exiting emacs:

(global-set-key (kbd "C-M-q") 'save-buffers-kill-emacs)

, but this binding exists in many local maps, so I have to override it:

(define-key lisp-mode-map (kbd "C-M-q") nil)

, but I want a command that overrides any local map without specifying which ones?;)

2 Answers 2

2

You can use a translation keymap to pretend that when Emacs receives the input C-M-q, it's actually the encoding of a special function key. Since you want this to apply to all terminals and to override mode-specific bindings, put it in key-translation-map.

(define-key key-translation-map [?\e ?\C-q] [fake-key-C-M-q])

You can then bind the function key fake-key-C-M-q to whatever you like. It will have this effect everywhere. You can override that binding in specific modes if you like, for example the code below makes the key exit Emacs except while in a minibuffer.

(global-set-key [fake-key-C-M-q] 'save-buffers-kill-emacs)
(define-key minibuffer-local-map [fake-key-C-M-q] 'ignore)
5
  • 3
    ` [?\e ?\C-q]` can be replaced with (kbd "C-M-q"). This kbd version is more understandable.
    – user31220
    Commented Oct 1, 2022 at 12:45
  • ok, but now I have problems if I use sequence, like this: (global-set-key [b0ef-C-M-l d] 'duplicate-current-line-or-region) Commented Oct 2, 2022 at 21:59
  • @JasonHunter I don't know exactly what you want to do, and “I have problems” is not a useful problem description. If you want C-M-l to be a prefix except in certain modes, that may be possible, but you'll need to work harder and involve function-key-map. Commented Oct 3, 2022 at 17:38
  • right, I want C-M-l to be a prefix in all modes. It won't accept (global-set-key [b0ef-C-M-l d] 'duplicate-current-line-or-region), written in that form. Well, I guess it does evaluate, but when I hit that combination nothing happens. First of all, is that supposed to work? I hit C-M-l and then "d" Commented Oct 3, 2022 at 20:13
  • @JasonHunter Your binding is for the (fake) function key called C-M-l followed by the function key called d, but there's no function key called d. You need to use the character d: (global-set-key [b0ef-C-M-l ?d] …) or (global-set-key (kbd "<b0ef-C-M-l> d") …) Commented Oct 3, 2022 at 21:19
0

Check out after-change-major-mode-hook; functions in this hook run whenever the major mode is set.

In my startup file I defun a function called fish then call:

(add-hook 'after-change-major-mode-hook 'fish)

In fish I call local-set-key for the keys I want to make sure are the same everywhere. (Technically, I call through some custom set-key code that makes things simpler in my mind... my Emacs setup is pretty crufty.)

Your Answer

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

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