This question already has an answer here:

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?

marked as duplicate by Dan Mar 11 '17 at 15:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


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.