2011-09-26 47 views

回答

11

甚至更​​少我不認爲你可以配置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!三次。

+0

完美地工作。謝謝! – TheEzEzz

+4

另請參閱Emacs'repeat'軟件包。 –

+0

@event_jr不錯!謝謝你的提示。 –

1

編號序列「ctrl-d ctrl-j」是什麼綁定到字符串「你好!」 Emacs將整個序列綁定到給定的字符串。下面是關於該主題的一些好消息:

http://xahlee.org/emacs/keyboard_shortcuts.html

在另一方面,如果你的「你好!」只是三個實例,你可以定義序列C-d C-j C-d C-j C-d C-j爲「你好你好!你好!「,但它會縮短爲你想要的字符串定義一個更簡單的序列。

+0

如果我綁定到'C-D'結合的功能「你好!」到'C-j'。然後每增加一次我按'C-j'我就會得到「你好!」。此外,我需要一些方法來監聽「d」鍵何時被釋放,以便我可以解除「Hello!」的綁定。從'C-j'。 這可能嗎? – TheEzEzz

+0

如果你打算這麼做,爲什麼不把C-j綁定到「你好!」 ? –

+0

因爲我想按'C-s C-j'時想要發生不同的事情。 – TheEzEzz

4

如果您正在尋找一些適用於所有命令的泛型,我無法看到這將如何工作 - emacs如何知道您是在開始一個新命令還是想重複之前的命令。一個更好的例子是「C-c h」,如果你在那之後輸入「h」,emacs應該重複命令還是插入h?

也就是說,emacs已經有一個機制 - 這是一個普遍的論點。

試試這個鍵序列:

C-u 3 C-d C-j 

這是按鍵比C-d C-Ĵç-J C-Ĵç-J

+0

我的玩具例子可能太玩具了。我試圖將JKLI重新映射爲我的導航鍵,以便'C-J'將光標左移,'C-L'右移,'C-I'上移,'C-K'移下來等等。我想覆蓋這個行爲,所以如果按下'd',那麼'C-J'刪除左邊,'C-L'刪除右邊等。 – TheEzEzz

2

嘗試這樣:

(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

+0

我想你應該推回最後一個終止while循環的事件,以免失去按鍵。請參閱丹尼爾布羅克曼的回答,儘管我很喜歡這個答案的清晰度。 – tripleee

+0

是的,你說得對,謝謝你的改正 –

0

我用這一切的時候服用。您需要庫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-cmdswide-n

0

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"))))