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?

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

    You could make your own wrappers around async-shell-command and shell-command to autofill with your last command + have your whole history. Try the code below to see if it does the job like you’d like.

    (defvar async-shell-command-history nil
      "List of commands executed with `async-shell-command`.")
    
    (defvar shell-command-history nil
      "List of commands executed with `shell-command`.")
    
    (defadvice async-shell-command (before save-command-in-history activate)
      "Save the command executed with `async-shell-command` in its history."
      (let ((command (ad-get-arg 0)))
        (unless (string-blank-p command)
          (setq async-shell-command-history (cons command async-shell-command-history)))))
    
    (defadvice shell-command (before save-command-in-history activate)
      "Save the command executed with `shell-command` in its history."
      (let ((command (ad-get-arg 0)))
        (unless (string-blank-p command)
          (setq shell-command-history (cons command shell-command-history)))))
    
    (defun my-async-shell-command ()
      "Run `async-shell-command` with a choice from its command history."
      (interactive)
      (let* ((command (completing-read "Async shell command: "
                                       async-shell-command-history
                                       nil nil
                                       (car async-shell-command-history)))
             (final-command (if (string-blank-p command) 
                                (or (car async-shell-command-history) "")
                              command)))
        (async-shell-command final-command)))
    
    (defun my-shell-command ()
      "Run `shell-command` with a choice from its command history."
      (interactive)
      (let* ((command (completing-read "Shell command: "
                                       shell-command-history
                                       nil nil
                                       (car shell-command-history)))
             (final-command (if (string-blank-p command) 
                                (or (car shell-command-history) "")
                              command)))
        (shell-command final-command)))
    
    • nqminhuit@alien.topOPB
      link
      fedilink
      English
      arrow-up
      1
      ·
      11 months ago

      Thank you very much for your effort, but I have found a neat way to achieve this, I updated answer on the post.