'How to map "jj" to Esc in emacs Evil mode

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)


Solution 1:[1]

This works for me. It requires the KeyChord library:

;;Exit insert mode by pressing j and then j quickly
(setq key-chord-two-keys-delay 0.5)
(key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
(key-chord-mode 1)

It is inspired by @phils answer above and based on Simon's Coding Blog: Emacs and Unity Every Day.

Solution 2:[2]

I don't know whether it works with Evil, but for Emacs in general the KeyChord library is designed for this sort of thing.

Try it and see?

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

Solution 3:[3]

If you're using Spacemacs then I just found that this setting (added to the beginning of user-init) works very well,

(setq-default evil-escape-key-sequence "jj")

Solution 4:[4]

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))))))))

Solution 5:[5]

For my windows install, adding as part of use-package evil configuration worked for me in init.el:

(use-package evil
 :ensure t
 :config
 (evil-mode 1)
 (define-key evil-insert-state-map "jj" 'evil-normal-state)
)

For Ubuntu, I followed E. Sambo's answer.

Solution 6:[6]

It's a bit more complicated - you have to watch for the previous character. This should do the trick. (the gist is for "jk", you can easily modify it for "jj" though you will note that "jk" is more efficient/faster).

Solution 7:[7]

This is my own solution i've been using for some time, although i use `jf' actually.

(defun xwl-jj-as-esc ()
  (interactive)
  (if (memq evil-state '(insert replace))
      (let ((changed? (buffer-modified-p)))
          (insert "j")
          (let* ((tm (current-time))
                 (ch (read-key)))
            (if (and (eq ch ?j)
                     (< (time-to-seconds (time-since tm)) 0.5))
                (save-excursion
                  (delete-char -1)
                  (evil-force-normal-state)
                  (set-buffer-modified-p changed?))
              (insert ch))))
    (call-interactively 'evil-next-line)))

(define-key evil-insert-state-map  "j" 'xwl-jj-as-esc)
(define-key evil-replace-state-map "j" 'xwl-jj-as-esc)

Solution 8:[8]

Initially I naively shared the answer below which not only it doesn't work (since it "disables" j and k) but also many emacs folks would laugh!

I found that evil-mode provides evil-define-key according to https://evil.readthedocs.io/en/latest/keymaps.html#evil-define-key.

So I tried below and it works for me.

;; you should be able to do "jj" instead but I prefer "jk"
(evil-define-key 'insert 'global (kbd "jk") 'evil-normal-state)

Now I found a real answer that is satisfying to me and working (using https://github.com/noctuid/general.el)

;; Enable installation of packages from MELPA.
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(unless package-archive-contents
  (package-refresh-contents))

;; Download Evil
(unless (package-installed-p 'evil)
  (package-install 'evil))

;; Enable Evil
(require 'evil)
(evil-mode 1)

;; ------------------------------------------------------
;; you might have all the above already 
;; but I still included for the completeness of the code
;; ------------------------------------------------------

;; Download general.el
(unless (package-installed-p 'general)
  (package-install 'general))

;; Enable general.el
(require 'general)
(general-evil-setup)

;; now usable `jk` is possible!
(general-imap "j"
  (general-key-dispatch 'self-insert-command
    :timeout 0.25
    "k" 'evil-normal-state))

since I literally just discovered general.el, this code might not be optimal

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 E. Sambo
Solution 2 phils
Solution 3 Caoilte
Solution 4
Solution 5
Solution 6 user673592
Solution 7 xwl
Solution 8