2016-04-07 Hiding those annoying Async Shell Command buffers

I use async-shell-command (M-&) quite often. (I usually ran just shell-command, by default bound to M-!, and added an ampersand at the end, until I learned about M-&.) However, when I do it, one thing annoys me a lot: the *Async Shell Command* buffers. Most of the time I don’t really care about them, since I do not want to see any standard output from async commands (like evince, a pdf viewer, or libreoffice, an office suite). Of course, this is Emacs, so I should be able to configure that, right?

Right.

Let’s start with the async-shell-command-buffer option. The default value is the symbol confirm-new-buffer, which means ask the user each time Emacs wants to create a new async buffer when one is already in use. Totally useless for me, so I set it to new-buffer, which means silently choose a new name and don’t bother me with silly questions.

Now to hide these buffers, we must first know where they come from. The function responsible for displaying them is aptly called display-buffer. It uses a complicate set of rules to decide how to actually display the given buffer, and there are a few ways of hooking into that heuristic. One which seems the most appropriate for our case is the display-buffer-alist option. As the name suggests, it is an association list whose keys are criteria for buffers and values are descriptions of how to display a particular buffer.

The criterion (in its simplest form) is a string, which is just a regex matching the buffer name. For us, the value of "^*Async Shell Command*" will be just fine. (The names of buffers generated by async-shell-command consist of the string *Async Shell Command* and an optional number in angle brackets; see e.g. the code of the function rename-uniquely for details.)

The value for each key-value pair is in general complicated, but happily it can be just a list with one element: a name of a function which will be used to display the buffer in question. Emacs provides a reasonable set of such functions, and we will need one of them: display-buffer-no-window (which actually does not display the buffer at all).

Summing up, here’s what we may do:

(add-to-list 'display-buffer-alist '("^*Async Shell Command*" . (display-buffer-no-window)))

This is not the end of the story, though. It might happen that we actually wanted to display the buffer with the output of our async command. It would be very natural to use the prefix argument for that. However, the prefix argument to async-shell-command already has a meaning (and a weird one, too): it means just forget about the whole asynchronous stuff and call the command synchronously. This is really strange, and I guess changing the meaning of the prefix argument to the default behavior of showing the output buffer may be a good idea.

This, however, will have to wait for another post.

CategoryEnglish, CategoryBlog, CategoryEmacs