1

Im trying make a new key binding, but i get conflicts with modes redefining this key.

After a good hour googling, what I think I want to do is:

(eval-after-load ANY_MODE
    (define-key (current-global-map) (kbd "C-M-h") 'shrink-window-horizontally))

So is there a way to do this? Is there even anything like ANYMODE? Or is there another way?

Mattis
  • 23
  • 3
  • 1
    @Tyler: +1 and I second that comment... The clean way to solve that problem is to define your own minor-mode containing all your keymappings (mode which you can then turn on and off at will) and to make sure that minor-mode keeps precedence. You definitely need to read the two most voted answers in the link that Tyler gave: http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs – TacticalCoder Sep 15 '14 at 14:43

2 Answers2

2

In modern Emacs versions, all programming modes inherit from prog-mode, text-related modes from text-mode, and some of the others from special-mode. You can add a hook function (that sets (or unbinds) a local key) to prog-mode-hook, text-mode-hook, and special-mode-hook, that way it will be executed for most major modes. The remaining ones you could manage on a case-by-case basis.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
1

You can use global minor mode for this purpose. Minor mode setting has higher priority over global mode setting.

(define-minor-mode my-overriding-minor-mode
  "Most superior minir mode"
  t  ;; default is enable
  "" ;; Not display mode-line
  `((,(kbd "C-M-h") . shrink-window-horizontally)))
syohex
  • 2,293
  • 17
  • 16
  • For the sake of avoiding confusion, I would avoid naming your mode `overriding-minor-mode`. For starters it's no more "overriding" than any other minor mode; and secondly there are existing concepts, such as the `minor-mode-overriding-map-alist` variable, which are close enough in name to cause confusion (note that you've just defined `overriding-minor-mode-map`). – phils Sep 16 '14 at 10:59
  • In any case, using a common prefix for name spacing purposes is [recommended](http://www.gnu.org/software/emacs/manual/html_node/elisp/Coding-Conventions.html). I like to use the prefix `my-` as (a) I'm pretty confident it won't clash; (b) it makes things nice and obvious to read; and (c) other people can copy my code and the name will work just as well for them. – phils Sep 16 '14 at 11:03