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
[–] samuelroy_@alien.top 1 points 1 year ago (1 children)

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.top 1 points 1 year ago

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