I want keybinding X to do one thing in c-mode and another thing in other modes. I'm trying to figure out how to do this in evil, specifically evil's insert-state.

I tried setting the keybinding in a mode hook:

(add-hook 'c-mode-hook (lambda ()
                         (define-key evil-insert-state-map (kbd "RET")

And left everything else defined in my init file as:

(define-key evil-insert-state-map (kbd "RET") 'comment-indent-new-line)

But the problem is that the hook does not seem to override my default keybinding. How do I override a key binding in evil based on a mode?


The standard way to set mode-specific keybindings in evil is to use:

;; default
(define-key evil-insert-state-map (kbd "RET") 'comment-indent-new-line)
;; for other mode
(evil-define-key 'insert c-mode-map (kbd "RET") 'c-indent-new-comment-line)

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