How to map jh and fd to <Esc> in Doom Emacs?

Viewed 2451

I'm aware of evil-escape-sequence but from what I can tell, you can only have one keybinding set to it. I want fd and jh both to bring me to Normal state from Insert state.

I've also tried something like (map! :i "jh" #'evil-normal-mode) but with this, whenever I type j, Emacs freezes and waits for me to complete the command.

3 Answers

I have done this with the keychord package. Note, this package measures the timeframe between the keystrokes in order to determine wether to trigger the paired function. This is how you can use h, j, k, l (keys that map to navigation in normal mode) for this extra functionality. Add (package! key-chord) to 'init.el' and the following code to 'config.el'.

(require 'key-chord)
(key-chord-mode t)
(key-chord-define-global "fd" 'evil-normal-state)
(key-chord-define-global "FD" 'evil-normal-state)
(key-chord-define-global "jh" 'evil-normal-state)
(key-chord-define-global "JH" 'evil-normal-state)

I added the capitalized equivalent so that you have the same functionality even when the caps lock is on.

If you do not want to use key-chord, there is another way:

(defun evil-insert-jk-for-normal-mode ()
  (interactive)
  (insert "j")
  (let ((event (read-event nil)))
    (if (= event ?k)
      (progn
        (backward-delete-char 1)
        (evil-normal-state))
      (push event unread-command-events))))
(define-key evil-insert-state-map "j" 'evil-insert-jk-for-normal-mode)

(Tested in doom-emacs v3.0.0-alpha.)

Related