11

I have a set of key bindings that I use for both web-mode and php-mode in insert mode (of Evil). So I cannot use set-local-map or something. Because in normal mode, the same keys will then call other commands. So the bindings need to be active in evil-insert-state-map only.

I have the following:

(eval-after-load 'php-mode
  '(progn
     (define-key evil-insert-state-map
       (kbd "C-<next>") (lambda () (interactive) (insert " = ")))
     (define-key evil-insert-state-map
       (kbd "C-<prior>") (lambda () (interactive) (insert "->")))))

(eval-after-load 'web-mode
  '(progn
     (define-key evil-insert-state-map
       (kbd "C-<next>") 'zencoding-expand-yas) 
     (define-key evil-insert-state-map
       (kbd "C-<prior>") 'zencoding-expand-line)))

So every time web-mode or php-mode is being loaded, these key bindings will be activated.

The problem is: Suppose I have two open buffers, one in the php-mode and the other in web-mode. When the web-mode buffer is the current buffer and I switch to an another buffer which's in php-mode, the web-mode bindings are still active inside the php-mode buffer.

I tried to solve this problem by using (add-hook 'web-mode-hook [...]) and (add-hook 'php-mode-hook [...]) instead of setting up the bindings in eval-after-load. But that solution doesn't work in this situation.

Any suggestions? I would really appreciate it.

1 Answer 1

13

Evil has the evil-define-key macro for defining commands for certain states in a specific keymap only:

(evil-define-key 'insert emacs-lisp-mode-map (kbd "C-c C-c") 'butterfly
                                             (kbd "<pause>") 'zone)

It is able to delay the execution of the binding (by using eval-after-load itself) and can also take more than one key-command pair which is a pretty nifty addition.

1
  • Your solution works, I'm grateful for that. Never knew there is such thing as evil-define key. And indeed pretty nifty that you could bind more keycommand pairs to the evil-define-key.
    – ReneFroger
    Apr 22, 2015 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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