【发布时间】:2012-05-13 03:40:03
【问题描述】:
最近我尝试了 Emacs,发现 Evil 有助于保持我的 vim 习惯。我习惯于像许多 Vimer 一样键入“jj”从插入模式返回正常模式,但不知道如何使其进入 Evil 模式。
我是这样映射的,但似乎不正确:
(define-key evil-insert-state-map (kbd "jj") 'evil-normal-state)
【问题讨论】:
最近我尝试了 Emacs,发现 Evil 有助于保持我的 vim 习惯。我习惯于像许多 Vimer 一样键入“jj”从插入模式返回正常模式,但不知道如何使其进入 Evil 模式。
我是这样映射的,但似乎不正确:
(define-key evil-insert-state-map (kbd "jj") 'evil-normal-state)
【问题讨论】:
这对我有用。它需要KeyChord 库:
;;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)
它的灵感来自上面的@phils 答案并基于Simon's Coding Blog: Emacs and Unity Every Day。
【讨论】:
我不知道它是否适用于 Evil,但对于 Emacs,KeyChord 库通常是为这类事情设计的。
试试看?
(key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
【讨论】:
如果您使用的是 Spacemacs,那么我发现这个设置(添加到 user-init 的开头)效果很好,
(setq-default evil-escape-key-sequence "jj")
【讨论】:
查看这篇博文:http://zuttobenkyou.wordpress.com/2011/02/15/some-thoughts-on-emacs-and-vim/ 并搜索“cofi”。我自己使用“kj”版本,它就像 Vim 一样工作。
编辑:这是博客文章中的实际代码 sn-p:
(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))))))))
【讨论】:
对于我的 Windows 安装,在 init.el 中添加作为 use-package evil 配置的一部分对我有用:
(use-package evil
:ensure t
:config
(evil-mode 1)
(define-key evil-insert-state-map "jj" 'evil-normal-state)
)
对于 Ubuntu,我遵循 E. Sambo 的回答。
【讨论】:
jj 直接插入到你的缓冲区中,这远远不能令人满意。
这有点复杂 - 你必须注意前一个字符。 This should do the trick.(要点是“jk”,您可以轻松修改它为“jj”,尽管您会注意到“jk”更高效/更快)。
【讨论】:
这是我自己的解决方案,我已经使用了一段时间,虽然我实际上使用了 `jf'。
(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)
【讨论】: