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?

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.