4

When I run a command with async-shell-command, sometimes I would like to quickly rerun the command. I can do this by running async-shell-command again and using M-p to cycle backward through history, but I would prefer to just press g in the output buffer (matching Emacs standard refresh semantics). How can I activate this functionality?

Matthew Piziak
  • 6,038
  • 3
  • 31
  • 79

1 Answers1

3

You can try something like (guaranteed untested):

(defvar-local shell-command--command nil)
(advice-add 'async-shell-command :after #'my-set-revert-buffer)
(defun my-set-revert-buffer (command &optional outbuf &rest _)
  (with-current-buffer (or outbuf "*Async Shell Command*")
    (setq shell-command--command command)
    (setq revert-buffer-function #'my-async-shell-rerun)
    (use-local-map
     (let ((map (make-sparse-keymap)))
       (set-keymap-parent map current-local-map)
       (define-key map "g" #'revert-buffer))))))
(defun my-async-shell-rerun (&rest _)
  (async-shell-command shell-command--command (current-buffer)))

But I suggest you M-x report-emacs-bug and request this as a new feature. It can't be added as-is because currently those async buffers are in shell-mode where you can interact with the underlying process, so binding g to revert-buffer is problematic, but setting up revert-buffer-function at least should be doable without any problem.

Stefan
  • 26,404
  • 3
  • 48
  • 85