13

In emacs, how can I show shadowed/overridden key bindings for the current buffer? They won't show up when running describe-bindings (C-h b).

In other words: how can I see if the modes active in a buffer have conflicting key bindings?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Norswap
  • 11,740
  • 12
  • 47
  • 60
  • I guess a workaround is to use a minor mode like the one in http://stackoverflow.com/a/683575/789593 and see if a particular key has another command when that minor mode is switched off. An even cruder approach may be to start Emacs with `emacs -q` to see if a key in your configuration has another binding without your configuration. – N.N. Dec 20 '12 at 15:04
  • Unfortunately, this doesn't address the problem of conflicting minor modes: you'd have to test every possible pair of minor modes individually. – Norswap Dec 20 '12 at 15:46

2 Answers2

11

Just call describe-mode: C-hm

The majority of mode docstrings will display their keymaps, and the method used to list them here also tells you if a binding is shadowed.

It doesn't tell you what it's shadowed by, but of course that's trivial to check with C-hc or C-hk.

e.g.:

key             binding
---             -------
[...]
C-M-q           indent-sexp
  (that binding is currently shadowed by another mode)

That text is generated by the function substitute-command-keys which processes the mode docstring when the documentation function is called.

e.g.:

(substitute-command-keys "\\{lisp-interaction-mode-map}")

The following functions can also be useful:

(key-binding KEY &optional ACCEPT-DEFAULT NO-REMAP POSITION) ;; dominant binding
(global-key-binding KEYS &optional ACCEPT-DEFAULT)
(local-key-binding KEYS &optional ACCEPT-DEFAULT)
(minor-mode-key-binding KEY &optional ACCEPT-DEFAULT) ;; discover keymap(s)
phils
  • 71,335
  • 11
  • 153
  • 198
0

for the heck of it I did this:

(define-key c++-mode-map "\C-c\C-s" 'kaw-sort-projects)

and then did C-h b (to see the bindings). And got this output :

Major Mode Bindings:
key             binding
---             -------

C-c C-q     c-indent-defun
C-c C-s     kaw-sort-projects
C-c C-u     c-up-conditional
C-c C-w     subword-mode

so it does seem to be showing up.

Is this what you meant ?

created this function that gives you the previous value when you define a key

(defun define-key-warn (map key fxn)
  "Bind a key and give info message if already bound"
  (setq old-fxn (lookup-key map key))
  (if old-fxn
      (message "INFO: key %s was defined as %s" key old-fxn))

  (define-key map key fxn)
)
kdubs
  • 1,596
  • 1
  • 21
  • 36
  • 1
    What I'd like to see is the binding that was shadowed by C-c C-s. So for instance, if before invoking `define-key`, `C-c C-s` was bound to `my-command`, then I'd like to see `C-c C-s my-command (shadowed)` appear somewhere. – Norswap Dec 20 '12 at 14:54
  • ah. so we need a way to dump/access the default bindings for the mode. have to think about that a moment. – kdubs Dec 20 '12 at 15:30
  • looks like once you bind the key, it completely wipes any hint of the former key. Unbinding the key, leaves it empty. looks like I'm not going to be any help. – kdubs Dec 20 '12 at 15:44
  • @kdubs In case there is no solution the answer might be to explain why as you did. Maybe one could redefine the functions that bind keys to in addition to rebinding store the overridden key commands? That way one may look up what is overridden. – N.N. Dec 20 '12 at 15:55
  • true. you could spit on the command that you are over riding at the point of definition (least I think you could). – kdubs Dec 20 '12 at 19:22