1
votes

I want to exit evil-mode's operator pending state when I press "jk" in quick succession.

For example, If I press d, and then jk, nothing should be deleted and Emacs should be in normal mode.

I tried using key-chord.el to exit the operator mode like so, but it didn't work.

(key-chord-define evil-operator-state-map "jk" 'evil-force-normal-state)

A similar question was how to exit isearch with "jk": key chords in isearch. I think the solution might require a similar approach.

1

1 Answers

1
votes

This is a little hacky as it relies on <down> being bound to the same as j, but it's the best I can do with my current evil-knowledge. It should work properly in all cases, including repeats.

(define-key evil-operator-state-map "j" 'evil-operator-state-j)

(evil-define-command evil-operator-state-j ()   (save-excursion
    (let ((evt (read-event "Press k to exit operator state" nil 0.5)))
      (if (and (integerp evt) (char-equal evt ?k))
          (keyboard-quit)
        ;; assume <down> is bound to the same as j:
        (let* ((operator-string (substring (this-command-keys) 0 -1)) ; get the keys used to invoke the operator
               (new-macro (kbd (concat operator-string " <down>"))))  ; add " <down>" to the end instead of "j"
          (evil-force-normal-state)
          (execute-kbd-macro new-macro)
          (when (not (null evt))
            (push evt unread-command-events))))))) ; process any other key pressed within 0.5 seconds

If you find a bug or have any questions about how it works, just ask. :)