How to Override evil-collection Keybindings

Having spent at least an hour over the past couple days figuring out how to do this, I thought it would be worth putting up a clear explanation for future googlers of said question. It’s currently possible to find out how to do this via google, but the answer is at the bottom of a thread on a GitHub issue and I feel like it would be nice for the top result to just straight up ask and answer the question.

The Problem

For each mode specified in evil-collection-mode-list, evil-collection adds a with-eval-after-load call to bind the vim-like keys you love so much for that mode. The way with-eval-after-load works, perhaps unsurprisingly, is it runs code to define those keybindings after you open a buffer which has a mode included in evil-collection’s mode list. Which means any keys you define for that mode inside your config file will get overriden by keys defined by evil-collection.

The Solution

Fortunately evil-collection provides a hook that we can piggy-back on to define our own bindings after evil-collection defines its bindings. The hook is called evil-collection-setup-hook, and can be used like this, for instance, to add a binding to prodigy-view-mode:


(defun my-prodigy-view-mode-keys (&rest a)
  (evil-define-key 'normal prodigy-view-mode-map (kbd "q") 'quit-window))

(add-hook 'evil-collection-setup-hook 'my-prodigy-view-mode-keys)

And if you are using use-package, you can take advantage of some nice syntactic sugar, like this


(use-package prodigy
  :hook (evil-collection-setup . (lambda (&rest a)
                   (evil-define-key 'normal prodigy-view-mode-map (kbd "q") 'quit-window)))
  ;; other config
)
Matt Roll @mroll