Skip to content

Instantly share code, notes, and snippets.

@mookid
Last active November 5, 2018 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mookid/64941602b840ca5ce81e2b3017d1e0a5 to your computer and use it in GitHub Desktop.
Save mookid/64941602b840ca5ce81e2b3017d1e0a5 to your computer and use it in GitHub Desktop.
emacs: repeatable interactive commands
;; I happen to use a lot the command `zap-to-char', which prompts for
;; a character, and deletes from the point to the character,
;; included. However, it is not repetition friendly: when you call the
;; `repeat' command, you are prompted again for the character.
;; So, if you would like to zap to the character # four times, you
;; have to press
;; M-z # M-z # M-z # M-z # ; call repeatedly `zap-to-char'.
;; or
;; M-z # C-x z # z # z # ; zap-to-char, the call repeatedly `repeat'.
;; Hopefully, there is another way, without redefining `zap-to-char':
(let (my-zap-to-char-last-arg)
(defun my-zap-to-char () ; make sure the command has no interactive argument
"Repetition friendly version of `zap-to-char'."
(interactive)
(let ((arg (if (eq last-repeatable-command 'my-zap-to-char)
my-zap-to-char-last-arg
(setq my-zap-to-char-last-arg (read-char "zap to char: ")))))
(zap-to-char 1 arg))))
(define-key global-map [remap zap-to-char] 'my-zap-to-char)
;; This works only in lexical binding mode. Otherwise you need to
;; define a global variable.
;; Now you can press:
;; M-z # M-z M-z M-z
;; or
;; M-z # C-x z z z
@didibus
Copy link

didibus commented Nov 5, 2018

Weird, I thought repeat worked even for functions that took interactive input. This is disappointing.

It seems C-x ESC ESC can repeat zap-to-char with its input also. The issue is, C-x ESC ESC is itself not repeatable. Would have been nice otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment