Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two key shortcut in emacs without repressing the first key?

Tags:

emacs

elisp

Suppose I define the following shortcut

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

Is it possible to configure emacs so that if I type "C-d C-j C-j C-j" I will get "Hello! Hello! Hello!" rather than having to type "C-d C-j C-d C-j C-d C-j"?

like image 537
TheEzEzz Avatar asked Sep 26 '11 19:09

TheEzEzz


People also ask

How do I set key bindings in Emacs?

The manual way is to use C-h k KEY to describe what KEY is bound to; the other way is to describe a function, FUNCTION , with C-h f FUNCTION . Because Emacs is a self-documenting editor all functions, variables, keys, etc. known to Emacs are accessible through the describe-xxx commands. Type C-h C-h to see them all.

How do I get help in Emacs?

Try 'C-h C-h' ( 'help-for-help' ) to access Emacs help. You can also use the 'Help' menu. The EmacsManual is available through 'Help' → 'Info' → 'Emacs' . The command names associated with the individual help keys can sometimes help you remember the keys themselves.

What is the Search command in Emacs?

Simple Searches Emacs also offers a simple, or nonincremental, search. To use a more straightforward search, type C-s RETURN or select Search from the Search menu. Type the search string, press RETURN, and Emacs begins the search. Simply press C-s again to repeat the search.

How do I open GNU Emacs?

To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt. To quit Emacs permanently, type C-x C-c.


1 Answers

I don’t think you can configure Emacs so that it does that for all commands. However, you can implement this functionality in the commands themselves. This is what is done for C-x e. Here is a macro I just wrote (guided by the standard definition of kmacro-call-macro in GNU Emacs 23.1.1) that makes it easy to add this functionality 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 "C-c x y z") 'hello-world)

Now you can type C-c x y z z z to insert Hello, World! three times.

like image 70
Daniel Brockman Avatar answered Oct 30 '22 18:10

Daniel Brockman