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?

  • abbreviatedman@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 months ago

    I like your thinking!

    Here’s a solution for automatically running the last command:

      (defun repeat-most-recent-shell-command-asynchronously ()
        (interactive)
        (async-shell-command (car shell-command-history)))
    

    It has two drawbacks:

    • shell-command-history doesn’t differentiate between asynchronous and synchronous shell commands, so you may accidentally repeat an async command synchronously (if you differentiate at all)—there are ways to look through the history for the one you want, however
    • you’re repeating automatically, not auto filling and giving yourself the chance to decide you want to run a different command instead

    Honestly probably not worth solving that second issue, as you could bind a function like the one above and then just use that binding instead of the usual when you want to repeat. (Actually, now that I’ve thought this through, I think that’s exactly what I’m gonna do.)

    If you want the auto fill behavior and not the repeat behavior, you could advise the shell-command functions to grab (car shell-command-history) and insert it into the minibuffer.

    Thanks for the thought-provoking question! I think I’m gonna go add this function now. : )