I like to work with a vertically split frame with code in both windows. When i call M-x compile the *compilation* buffer shows in the other window (wich is fine as M-g n would show problematic code in the current window while i can still see the error in the other), but, after reviewing code, i would like to kill the *compilation* buffer and show the code that was in that window before, without visiting that window (C-x o) and killing it (C-x k).

Is there a way to do that?

I've come up with something, altough, it does not work; it throws:

wrong-type-argument windowp (#<window 10 on *Backtrace*>))`
window-buffer((#<window 10 on *Backtrace*>))

I believe that window-buffer does take a window as an argument and return its buffer, but I don't know any Lisp.

The code:

(defun kill-other-window-buffer ()
  "Kill the buffer of the other window if there is two windows"
  ;; I thought it will be better this way, to avoid killing an unexpected buffer
  ;; when several windows are in the frame
  (if (= (length (window-list)) 2)
      (window-buffer (last (window-list)))
  • 3
    Doesn't pressing C-x k prompt you to chose the buffer you want to kill from the buffers list ? You can just chose the buffer you want to kill and press enter to kill buffer. Sep 17, 2016 at 16:47
  • What @ChakravarthyRaghunandan said.
    – Drew
    Sep 17, 2016 at 19:14
  • Are you looking for something like this?
    – Nsukami _
    Sep 17, 2016 at 22:19
  • 1
    How about?: (kill-buffer "*compilation*")
    – lawlist
    Sep 18, 2016 at 5:36

2 Answers 2


As suggested by Charkravarthy and Drew, using either vanilla Emacs, or together with Helm, Ido or Ivy one can kill / close the *Compilation* buffer using C-x k and selecting it when prompted to do so.


I found the issue in the original function written by the author of the question. The car function should wrap the last function because last returns a list.

 (if (= (length (window-list)) 2)
      (window-buffer (car (last (window-list))))

However, after using this function, I found that (of course) it wouldn't work if there were 3+ windows open.

There's also M-x winner-undo which restores the previous window configuration.

I tested this with 2 windows open vertically, with FileA and FileB. I compiled FileA and the compilation buffer appears with no errors, hiding FileB. I ran M-x winner-undo and File B was again visible.

I also tested with a single window FileA. I ran compile, and the compilation results appear in a new window below FileA. M-x winner-undo restored FileA to be the only visible window.

There's many ways to do the same thing and it's a choice between whether you want to expressly kill the Compilation buffer or kill the most recent buffer shown in the most recent window.

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.