Is it possible to make a few different commands share a window for their output? For example, let's say I do M-x compile and get a *compilation* buffer in bottom half of my frame. If I then do M-x ack (or M-x grep, etc.), I would like the results of these commands to be displayed in the same window which *compilation* occupies.

Some background: In reply to this question @Francesco provided a nice snippet which displays the compilation window at the bottom of the current frame. This works nicely to manage *compilation*, but it would be even nicer if compile, ack, ag, igrep and a few similar commands shared this window.

(So what I am really looking for is how to extend Francesco's snippet to manage the output of multiple commands together.)

1 Answer 1


I think something like this should work (independently of the snippet you mention to always create compilation windows at the bottom of the current frame).

Whenever you try to display a compilation buffer (or any buffer in a derived mode), display-buffer-reuse-compilation-window will try to find another currently displayed compilation buffer, and reuse the window. If there is no compilation buffer currently displayed, the standard action will be taken.

(require 'dash)

(defun display-buffer-reuse-compilation-window (buffer param)
  (message "toto")
  (let ((candidate
         (-find (lambda (b)
                   (get-buffer-window b)
                   (with-current-buffer b
                     (derived-mode-p 'compilation-mode))))
    (when candidate
      (with-selected-window (get-buffer-window candidate)
        (switch-to-buffer buffer)))))

(add-to-list 'display-buffer-alist
              ;; Condition
              (lambda (buffer action)
                (with-current-buffer buffer
                  (derived-mode-p 'compilation-mode)))
              ;; Action

A bit of warning, though: I'm not sure my snippet complies with all display-buffer options regarding e.g. the possibility to reuse windows in other frames.

  • Thank you. I am going to try to merge this with stackoverflow.com/questions/28309340/… (your code shows how to check whether two buffers are compatible, there there is more fine-tuned frame-related logic).
    – Mekk
    Commented Feb 6, 2015 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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