How can I tell emacs not to pop up the *Shell Command Output*
buffer when calling a shell command like this?
(shell-command MY_COMMAND)
Currently emacs splits the current window into two, showing the (mostly irrelevant) output buffer. To me it would be completely sufficient if I could look it up later if I feel like it.
Maybe using shell-command
was the root of the problem. I think I found a solution with call-process
which works, although there may be a more elegant way:
(call-process-shell-command
"cat ~/.emacs.d/init.el"
nil "*Shell Command Output*" t
)
In my experience, if the shell command itself produces no output, then the emacs *Shell Command Output*
buffer won't pop open.
Therefore, to avoid the output buffer, silence the output of the command.
One easy way is:
" > /dev/null 2>&1"
to the end of any shell command. (Caveat: I'm unsure if /dev/null
exists on 100% of platforms where one can run emacs, but on every Linux distro it should be fine.)
If the call to elisp function shell-command
is in an elisp script, then you could change this:
(shell-command cmd)
to this:
(shell-command (concat cmd " > /dev/null 2>&1"))
If you occasionally do want to monitor the output, then you could create one wrapper function that suppresses the output via /dev/null
, and one wrapper function with no suppression, and toggle between them as you wish.
The above advice was tested on: GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) of 2017-09-20 on lcy01-07, modified by Debian
shell-command
takes an optional argument OUTPUT-BUFFER
where you can specify the buffer to output to. If it is t
(actually not a buffer-name and not nil
) it will be output in the current buffer. So we wrap this into a with-temp-buffer
and will never have to bother with it:
(with-temp-buffer
(shell-command "cat ~/.emacs.d/init.el" t))
This utility function might help. It returns the actual value of the shell command
(defun shell-command-as-string (cmd)
(with-temp-buffer
(shell-command-on-region (point-min) (point-max)
cmd t)
(buffer-string)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With