Two key shortcuts in emacs without suppressing the first key?

Suppose I define the following shortcut

(global-set-key (kbd "Cd Cj") "Hello!")

Is it possible to configure emacs so that if I type "Cd Cj Cj Cj" I get "Hello! Hello! Hello!" instead of typing "Cd Cj Cd Cj Cd Cj" ?

+8
emacs elisp
source share
6 answers

I do not think that you can configure Emacs to execute this for all commands. However, you can implement this functionality in the commands themselves. It is made for Cx e . Here is the macro I just wrote (guided by the standard definition of kmacro-call-macro in GNU Emacs 23.1.1) , which makes it easy to add this function to your own commands:

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

Here's how you use it:

 (defun hello-world () (interactive) (with-easy-repeat (insert "Hello, World!\n"))) (global-set-key (kbd "Cc xyz") 'hello-world) 

Now you can enter Cc xyzzz three times to paste Hello, World! .

+11
source share

If you are looking for something in common that works on all teams, I donโ€™t see how it will work - how emacs will know if you are starting a new team or want to repeat the previous one. A better example would be "Cc h", if after that you type "h", should emacs repeat the command or paste h?

However, emacs already has a mechanism for this - a universal argument.

Try this key sequence:

 Cu 3 Cd Cj 

This is even less keystroke than Cd Cj Cj Cj Cj

+4
source share

Try something like this:

 (global-set-key (kbd "Cc Cj") (lambda() (interactive) (insert "Hello!") (message "Type j to print Hello!") (while (equal (read-event) ?j) (insert "Hello!")) (push last-input-event unread-command-events))) 

Idea taken from kmacro-call-macro

+2
source share

Not. The sequence "ctrl-d ctrl-j" is what is associated with the string "Hello!" Emacs associates the sequence as a whole with this string. Here is some good information on the topic:

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

On the other hand, if you want only three instances of โ€œHello!โ€, You can define the sequence Cd Cj Cd Cj Cd Cj be โ€œHello! Hello! Hello!โ€, But it would be easier to just define a simpler sequence for the desired line.

+1
source share

I use it all the time. To use it, you will need the repeat.el library (part of 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))

Now bind my-hello-repeat :

(global key set (kbd "") 'my-hello-repeat)

Now just press the home key to repeat HELLO !.

I use this technique in several libraries, including Bookmark + , thing-cmds and wide-n .

0
source share

smartrep is all you need

 (require 'smartrep) (defvar ctl-d-map (make-keymap)) ;; create Cd map (define-key global-map "\Cd" ctl-d-map) (smartrep-define-key global-map "Cd" '(("Cj" . (insert "hello")))) 
0
source share

All Articles