Some of the packages I use define keybindings that I want to overwrite or remove. How do I overwrite/remove these keybindings?

For example, I use ace-window and speedbar together. ace-window uses M-p for switching windows, and I want to keep that binding. However, it's used by speedbar for speedbar-restricted-prev with (define-key map "\M-p" 'speedbar-restricted-prev) in the source).

I don't know what speedbar-restricted-prev does and I don't think I will be using it. This is how I have configured ace-window. What else do I need to do to disable the binding of M-p in speedbar? Outside of speedbar it works.

(use-package ace-window
  :ensure t
  :defer t
    (global-set-key (kbd "M-p") 'ace-window)
    (setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
     ;;more info at https://github.com/abo-abo/ace-window

1 Answer 1

(define-key speedbar-mode-map "\M-p" nil)

should remove the binding of M-p from the speedbar map, so that it no longer interferes with your binding for ace-window.

You'll need to call this after speedbar-mode is loaded for it to have effect. Since you're using use-package, you can accomplish this by adding it as a :config option, e.g.,

(use-package sr-speedbar
  :ensure t
  :defer t
  :config     (define-key speedbar-mode-map "\M-p" nil)

More generally, you can use eval-after-load:

(eval-after-load "speedbar"
    '(define-key speedbar-mode-map "\M-p" nil))
  • [pastebin of current config[(pastebin.com/niciAQcc) - At what point in the cycle should the define-key be executed. I am aware of add-hook options but don't know how to use them. Would putting the above command after the (sr-speedbar-open) be okay? How would I guarantee that the setting in ace-window would came into effect only after your define-key had been applied?
    – vfclists
    Commented Aug 4, 2017 at 16:41
  • With this config, it doesn't matter if the ace-window setting comes before or after speedbar.
    – Tyler
    Commented Aug 4, 2017 at 17:21

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