emacs中的两个关键快捷键而不压缩第一个键?

The*_*Ezz 8 emacs elisp

假设我定义了以下快捷方式

(global-set-key (kbd "C-d C-j") "Hello!")

是否可以配置emacs,这样如果我输入,"C-d C-j C-j C-j"我将得到"你好!你好!你好!" 而不是必须打字"C-d C-j C-d C-j C-d C-j"

Dan*_*man 11

我不认为您可以配置Emacs以便它为所有命令执行此操作.但是,您可以在命令本身中实现此功能.这就是为此所做的C-x e.这是我刚刚编写的一个宏(由kmacro-call-macroGNU Emacs 23.1.1中的标准定义指导),可以很容易地将此​​功能添加到您自己的命令中:

(defmacro with-easy-repeat (&rest body)
  "Execute BODY and repeat while the user presses the last key."
  (declare (indent 0))
  `(let* ((repeat-key (and (> (length (this-single-command-keys)) 1)
                           last-input-event))
          (repeat-key-str (format-kbd-macro (vector repeat-key) nil)))
     ,@body
     (while repeat-key
       (message "(Type %s to repeat)" repeat-key-str)
       (let ((event (read-event)))
         (clear-this-command-keys t)
         (if (equal event repeat-key)
             (progn ,@body
                    (setq last-input-event nil))
           (setq repeat-key nil)
           (push last-input-event unread-command-events))))))
Run Code Online (Sandbox Code Playgroud)

以下是您使用它的方式:

(defun hello-world ()
  (interactive)
  (with-easy-repeat
    (insert "Hello, World!\n")))

(global-set-key (kbd "C-c x y z") 'hello-world)
Run Code Online (Sandbox Code Playgroud)

现在您可以键入三次C-c x y z z z插入Hello, World!.

  • 另请参阅Emacs`repeat`包. (4认同)