Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the *shell command output* buffer be kept in the background?

Tags:

emacs

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.

like image 208
quazgar Avatar asked Jul 23 '12 13:07

quazgar


4 Answers

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
 )
like image 105
quazgar Avatar answered Nov 03 '22 00:11

quazgar


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:

  • add " > /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

like image 20
pestophagous Avatar answered Nov 03 '22 00:11

pestophagous


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))
like image 45
pmr Avatar answered Nov 03 '22 00:11

pmr


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)))
like image 1
istib Avatar answered Nov 03 '22 01:11

istib