2

In my init file I have a function that runs a shell-command everytime I open emacs. I rarely need to look at the out put of this command, so having it pop up in the *Shell Command Output* buffer every time I open emacs is unnecessary, and also means that I always have to take an extra step to get rid of that window.

From the documentation for shell-command, I have learned that I can provide an OUTPUT-BUFFER argument and direct the output where I want it to go. Sending it to the *Messages* buffer is almost the perfect solution for me, because it means I don't need to think about the output of the script, but if I do need to find it, I can find it in *Messages*, which seems to be a logical place to look for it.

My only problem is that adding the OUTPUT-BUFFER argument will make it so that whatever messages are in the *Messages* buffer at the time the command is run will be replaced by the shell-command output. I rarely look at the *Messages* buffer unless I need to, but I want to make sure I don't remove anything important just in case. Is there any way for me to append the output of shell-command to this buffer rather than replacing the buffer's contents? Any push in the right direction would be appreciated. Thank you!

5

Here's one way to put the output of a command into the *Messages* buffer.

(defun my-shell-command-to-message (command)
  "Run a command in the shell and append its output to the *Messages* buffer."
  (message (shell-command-to-string command)))

Note that it also displays it at the bottom of the screen.

To append the output of a command to a named buffer you could use a function like my-shell-command-to-buffer below.

(defun my-shell-command-to-buffer (command buffer-name)
  "Run a `command' and append its output to buffer `buffer-name'."
  (with-current-buffer (get-buffer-create buffer-name)
    (goto-char (point-max))
    (insert (shell-command-to-string command))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.