this post was submitted on 18 Oct 2023
1 points (100.0% liked)

Emacs

310 readers
2 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
 

Hello, I usually run the same command with async-shell-command, however I have to navigate back to the last command history to trigger my last run command, is there any way async-shell-command (and shell-command) can autofill with last run command so that I just hit enter without extra steps?

you are viewing a single comment's thread
view the rest of the comments
[–] abbreviatedman@alien.top 1 points 1 year ago

I tweaked the answer somewhat, as it bothered me that we were getting the most recent command inserted and having it show up again when you decide to go back to an earlier command. In other words, with the OP's advice, we get the most recent command in the minibuffer history twice, once auto-filled and once in the regular history.

I'm sure there are cleaner ways to do it than using a custom global variable no other functions know about, which could I think lead to some weird side effects at some point... but for now, I like the behavior:

(defvar crj-most-recent-shell-command (car shell-command-history))

(defun crj--pop-shell-command-history (fn &rest _args)
  (setq crj-most-recent-shell-command (car shell-command-history))
  (let ((shell-command-history (cdr shell-command-history)))
    (apply fn _args)))

(defun crj--auto-fill-shell-commands (args)
  (list (car args) crj-most-recent-shell-command))

(advice-add 'read-shell-command :around #'crj--pop-shell-command-history)
(advice-add 'read-shell-command :filter-args #'crj--auto-fill-shell-commands)

Let me know if anyone has improvements/objections!