2

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")
                           'c-indent-new-comment-line)))

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?

Dan
  • 32,584
  • 6
  • 98
  • 168
ideasman42
  • 8,375
  • 1
  • 28
  • 105

1 Answers1

3

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)

N.b. c-mode-map is quoted.

kleinbottle4
  • 103
  • 3
ideasman42
  • 8,375
  • 1
  • 28
  • 105