How to map “jj” to Esc in emacs Evil mode

2019-03-08 18:21发布

Recently I tried Emacs and found Evil helpful to keep my vim custom. I'm used to typing "jj" to return normal mode from insert mode like many Vimers do but don't know how to make it in Evil mode.

I map it like this but seems not correct:

(define-key evil-insert-state-map (kbd "jj") 'evil-normal-state)

7条回答
SAY GOODBYE
2楼-- · 2019-03-08 18:52

See this blog post: http://zuttobenkyou.wordpress.com/2011/02/15/some-thoughts-on-emacs-and-vim/ and search for "cofi". I use the "kj" version myself and it works just like Vim.

EDIT: Here is the actual code snippet from the blog post:

(define-key evil-insert-state-map "k" #'cofi/maybe-exit)

(evil-define-command cofi/maybe-exit ()
  :repeat change
  (interactive)
  (let ((modified (buffer-modified-p)))
    (insert "k")
    (let ((evt (read-event (format "Insert %c to exit insert state" ?j)
               nil 0.5)))
      (cond
       ((null evt) (message ""))
       ((and (integerp evt) (char-equal evt ?j))
    (delete-char -1)
    (set-buffer-modified-p modified)
    (push 'escape unread-command-events))
       (t (setq unread-command-events (append unread-command-events
                          (list evt))))))))
查看更多
登录 后发表回答