• 0 Posts
  • 1 Comment
Joined 11 months ago
cake
Cake day: October 16th, 2023

help-circle
  • 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)))