When I navigate to a .pdf in dired-mode and hit !, it opens the .pdf in Okular, but the dired-buffer is split into two parts, the second one now being a useless *Async Shell Command* buffer containing content like

How can I prevent this buffer from being opened? (except for, maybe, if there was an error and this information is useful).

I found related questions here and here, but they seem to deal with specific commands executed asynchronously, instead of the *Async Shell Command* in general (if possible, I would like to change the behaviour in general for asynchronous processes, not only for certain file types)

Take a look at the actual functions inside the source of .../lisp/simple.el -- i.e., defun shell-command and defun async-shell-command. You can even create your own custom functions and/or use defalias. When using start-process, the second argument is the output buffer name -- using nil for the second argument prevents an output buffer from being created. You can use set-process-sentinel in conjunction with start-process.
–
lawlistOct 3 '13 at 15:19

The doc string of async-shell-command states: ... In Elisp, you will often be better served by calling 'start-process' directly, since it offers more control and does not impose the use of a shell (with its need to quote arguments).
–
lawlistOct 3 '13 at 15:30

But then you are forcing all uses of async-shell-command to lose the buffer. I'd think that would be overkill; there will certainly be situations where you do want to see the output. Changing the advice to add a parameter which specifies whether or not to do save-window-excursion should not be too hard, but then why use advice in the first place; use a simple wrapper function instead.
–
tripleeeDec 17 '12 at 4:40

... I do realize OP asked for a fix to async-shell-command specifically, but the caveats for messing with it should be emphasized.
–
tripleeeDec 17 '12 at 4:47

This did not work for me. I still get the Async Shell Command buffer.
–
Marius HofertMay 17 '13 at 11:33

That's because async-shell-command calls shell-command to do its magic.
–
lawlistOct 3 '13 at 15:26

Sadly there is no good way to avoid this buffer as it's called directly by 'shell-command' function ('async-shell-command' is just a wrapper).

So, a much better way is to replace 'async-shell-command' with 'start-process'.
You should start process with 'set-process-sentinel' to detect the moment when process emits 'exit signal. Then kill process.

The actual problem is with the okular program which apparently spews meaningless messages to its standard error. In the short term, create a wrapper (say, $HOME/bin/okular) which discards standard error, or filters out the obvious chaff. In the slightly longer term, file a bug report on okular or switch to a well-behaved PDF reader.

Thanks for your help. The problem is not limited to Okular, it only served as an example. The same happens with VLC, for example. It would be tedious to write a wrapper for every application (although there are not too many). With okular itself, the problem might appear since it's an KDE application I run on Ubuntu.
–
Marius HofertDec 17 '12 at 7:26