Derek Thurn
Derek Thurn

Reputation: 15355

Emacs automatically close async output buffers on command completion

I'm currently running an asynchronous emacs command with a fair degree of regularity like this:

(save-window-excursion
  (async-shell-command
    cmd
    (generate-new-buffer "async")))

This works well and all, but it clutters up my emacs instance with a whole ton of async<5> and async<11> buffers. How can I automatically kill these buffers when their corresponding asynchronous command finishes executing?

Upvotes: 2

Views: 1188

Answers (3)

Tobias
Tobias

Reputation: 11

Please have a look at http://news.gmane.org/find-root.php?message_id=%3cloom.20120517T145957%2d51%40post.gmane.org%3e

The second proposal there starts a sentinel along with the shell process. When this sentinel detects the process status 'exit you can kill the process buffer right away or you can start dying-mode for the process buffer as it is proposed in the cited posting.

Within the dying time you can inspect the process output, cancel dying or prolong the life-time of the buffer.

Best regards, Tobias

Upvotes: 1

zev
zev

Reputation: 3590

I'm assuming that (for this particular use-case) you are rarely interested in looking at the output that is put in the "async" buffer and that you just want to prevent the creation of the extraneous buffers. If so, you could do:

(save-window-excursion
  (when (get-buffer "async")
    (kill-buffer "async"))
  (async-shell-command
    cmd
    (generate-new-buffer "async")))

This will kill the "async" buffer prior to running the "async-shell-command" and thus prevent the additional "async" buffers from being created.

Upvotes: 0

Nathaniel Flath
Nathaniel Flath

Reputation: 16005

While it won't kill them when the command completes, you can have the buffers killed after a period of time - this assumes that the async commands are shotr-lived ( or have a fairly-known runtime). Something like:

(save-window-excursion
  (let ((buf (generate-new-buffer "async")))
    (async-shell-command cmd buf)
    (run-with-timer 10 nil (lambda (buf) (kill-buffer buf)) buf)))

Upvotes: 2

Related Questions