1
submitted 11 months ago by nqminhuit@alien.top to c/emacs@communick.news

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 11 months ago

Check the documentation of async-shell-command (M-x describe-function async-shell-command RET), and you'll see that it takes one necessary argument, the command to run.

If you run it interactively (through its keybinding or M-x), it prompts you for that argument. Type in the command for Emacs, and Emacs will pass that as the argument to async-shell-command.

But if you want to, you can call it from Lisp and pass in the argument yourself! Try executing it in the scratch buffer or by running eval-expression (I believe M-: by default).

(async-shell-command "git status")

That will run git status and give you the result

Now if you want to run that bit of code more often, wrap it in a function and assign it a keybinding!

(defun crj-check-git-status ()
  (interactive)
  (async-shell-command "git status"))

(general-define-key (kbd "C-M-&") #'crj-check-git-status)

Would anyone else handle this differently? I'm still learning myself!

[-] nqminhuit@alien.top 1 points 11 months ago

I think you misunderstood, I need autofilled with the last run command (which is dynamic) not the the hard-coded one.

[-] abbreviatedman@alien.top 1 points 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. : )

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

Emacs

310 readers
1 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