Search code examples
emacselisp

How do you control the output window of an Emacs Lisp shell command?


Given something like this:

(global-set-key (kbd "<f4>") (lambda () (interactive) (shell-command "gcc --version")))

Is there a way to get the output to always go into a "new or existing window on the right" versus sometimes doing that, and sometimes putting the output in a little window below the status and command areas (two different possibilities shown below)?

enter image description here


Solution

  • The following works reliable also for one line of output (e.g. "echo hello" instead of "gcc --version"):

    (global-set-key (kbd "<f4>")
                    (lambda () (interactive)
                       (with-current-buffer (get-buffer-create "*Shell Command Output*")
                         (erase-buffer)
                         (insert (shell-command-to-string "gcc --version"))
                         (display-buffer (current-buffer)))))