0

I use ace-window to change windows, with M-o as my key-binding.

However, when I enter a window that is in HTML mode, this gets clobbered to "set face." I'm now stuck in this window, and can't M-o my way back out.

I have the following in my ace-window config, which specifies the key is global:

(use-package ace-window
  :init
  (setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
  (setq aw-dispatch-always t)
  :config
  (global-set-key (kbd "M-o") 'ace-window)
  )

How can I stop html-mode for now, and /any/ mode in general, from clobbering the global keybinding?

I'm generally learning about key-bindings from here.

update

This was marked a duplicate but it’s not clear to me how to apply the links to this problem. I didn’t load markdown-mode in my init.el as these suggest editing—it automatically clobbered my keybindings with defaults without any loader.

What I’m looking for in this question is specifically how can I make the keybindings in ace-jump un-overwritable, even if rogue modes load themselves after.

Mittenchops
  • 329
  • 1
  • 9
  • This is a duplicate. See here, for example: https://emacs.stackexchange.com/questions/23800/emacs-ignoring-global-keybindings – Fran Burstall Jan 02 '23 at 08:40
  • See Searching the Active Keymaps: the global keymap is the last keymap to be looked at, so if a higher-precedence keymap binds the key, that's what you will end up with. So they don't really clobber the global keybinding: they just shadow it. – NickD Jan 02 '23 at 08:52
  • Thanks, I don’t understand how to use those links to answer my particular problem. Do I need to load the keybindings differently for ace-jump? I don’t even have markdown-mode in my init.el, so I don’t see what I have to write there. This was just default behavior. – Mittenchops Jan 02 '23 at 16:49
  • The link was meant to help you understand why that was happening, not to help you deal with the immediate problem (that's why it was a comment, not an answer). As for an answer, I suspect that you will have to play whack-a-mole with each mode that uses the M-o keybinding, Alternatively, you can find an unused key to bind ace-window to (M-o is just a suggestion). The dupe that @FranBurstall has pointed out tells you about the Emacs conventions for user-defined keybindings (basically C-c <letter> should never be used by packages, so that is a safe choice). – NickD Jan 02 '23 at 17:10
  • Is there any kind of immutable keybinding? Or is there hook where I could add a function that after any keybinding in a package, could call back my ace-jump global keybinding and reset it back to what I want? – Mittenchops Jan 02 '23 at 17:29
  • 1
    As to an immutable keybinding, the answer is "no", but that is not the problem you have (as I tried to explain): the keybinding in the global map is not changed - it is still there, but it is shadowed by a keybinding in a different map that is of higher priority. As for a hook, I don't think that packages do that in general, so even if some did, it would still be a whack-a-mole thing. Your best bet IMO is to use a "safe" keybinding in the global map (maybe C-c o to make it as similar to M-o if you are inclined that way). – NickD Jan 02 '23 at 17:58
  • 1
    In your update you say "What I’m looking for in this question is specifically how can I make the keybindings in ace-jump un-overwritable, even if rogue modes load themselves after." and as explained, there is no way to do that. Even with an overriding keymap as in @FranBurstall's answer, a "rogue" mode can add its own overriding keymap, so it can always throw a wrench into the machinery. That is why Emacs reserves some keys that by convention packages will not use, but even here a rogue package can flout the convention and cause havoc. – NickD Jan 03 '23 at 14:26

1 Answers1

2

Sorry: I was reluctant to add to the gazillion "global-set-key does not work" answers but the comments indicate that I did not say enough. Anyway, here is an actual answer:

  1. By now it should be clear that the global-keymap is at the bottom of the keymap hierarchy and will be overridden by almost any other keymap.
  2. There is however a solution for use-package users:
(use-package ace-window
  :demand t
  :init
  (setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
  (setq aw-dispatch-always t)
  :bind*("M-o" . ace-window))

This invokes the bind-key package which provides an over-riding keymap. Do C-h P bind-key for more information.

Fran Burstall
  • 3,855
  • 11
  • 18