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

前端 未结 7 1782
迷失自我
迷失自我 2021-01-31 07:46

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

7条回答
  •  别那么骄傲
    2021-01-31 08:42

    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)
    

提交回复
热议问题