假設我定義了以下快捷emacs中沒有壓制第一個鍵的兩個快捷鍵?
(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"
?
假設我定義了以下快捷emacs中沒有壓制第一個鍵的兩個快捷鍵?
(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"
?
甚至更少我不認爲你可以配置Emacs的,以便它對於所有的命令。但是,您可以在命令本身中實現此功能。這就是C-x e。 這裏是一個宏觀的我只是寫,可以很容易將此功能添加到您自己的命令(通過kmacro-call-macro
在GNU 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))))))
這裏是你如何使用它:
(defun hello-world()
(interactive)
(with-easy-repeat
(insert "Hello, World!\n")))
(global-set-key (kbd "C-c x y z") 'hello-world)
現在您可以鍵入抄送xyzzz插入Hello, World!
三次。
編號序列「ctrl-d ctrl-j」是什麼綁定到字符串「你好!」 Emacs將整個序列綁定到給定的字符串。下面是關於該主題的一些好消息:
http://xahlee.org/emacs/keyboard_shortcuts.html
在另一方面,如果你想的「你好!」只是三個實例,你可以定義序列C-d C-j C-d C-j C-d C-j
爲「你好你好!你好!「,但它會縮短爲你想要的字符串定義一個更簡單的序列。
如果您正在尋找一些適用於所有命令的泛型,我無法看到這將如何工作 - emacs如何知道您是在開始一個新命令還是想重複之前的命令。一個更好的例子是「C-c h」,如果你在那之後輸入「h」,emacs應該重複命令還是插入h?
也就是說,emacs已經有一個機制 - 這是一個普遍的論點。
試試這個鍵序列:
C-u 3 C-d C-j
這是按鍵比C-d C-Ĵç-J C-Ĵç-J
我的玩具例子可能太玩具了。我試圖將JKLI重新映射爲我的導航鍵,以便'C-J'將光標左移,'C-L'右移,'C-I'上移,'C-K'移下來等等。我想覆蓋這個行爲,所以如果按下'd',那麼'C-J'刪除左邊,'C-L'刪除右邊等。 – TheEzEzz
嘗試這樣:
(global-set-key (kbd "C-c C-j") (lambda()
(interactive)
(insert "Hello!")
(message "Type j to print Hello!")
(while (equal (read-event) ?j)
(insert "Hello!"))
(push last-input-event unread-command-events)))
理念從kmacro-call-macro
我想你應該推回最後一個終止while循環的事件,以免失去按鍵。請參閱丹尼爾布羅克曼的回答,儘管我很喜歡這個答案的清晰度。 – tripleee
是的,你說得對,謝謝你的改正 –
我用這一切的時候服用。您需要庫repeat.el
(GNU Emacs的一部分)才能使用它。
(defun make-repeatable (command) "Repeat COMMAND." (let ((repeat-message-function 'ignore)) (setq last-repeatable-command command) (repeat nil))) (defun my-hello() "Single `hello'." (interactive) (insert "HELLO!")) (defun my-hello-repeat() (interactive) (require 'repeat) (make-repeatable 'my-hello))
現在綁定my-hello-repeat
:
(全球設置鍵(KBD 「」),「我的問候重複)
現在只需按住家鍵重複HELLO !。
我在多個庫中使用了相同的技術,包括Bookmark+,thing-cmds和wide-n。
smartrep是所有你想要的
(require 'smartrep)
(defvar ctl-d-map (make-keymap)) ;; create C-d map
(define-key global-map "\C-d" ctl-d-map)
(smartrep-define-key
global-map "C-d"
'(("C-j" . (insert "hello"))))
完美地工作。謝謝! – TheEzEzz
另請參閱Emacs'repeat'軟件包。 –
@event_jr不錯!謝謝你的提示。 –