Say I have two existing files named "file1.el" and "file2.el".

Then I perform the following steps:

  1. I open a file named "file1.el"

  2. C-x C-f a file named "file2.el", now currently I have 1 window and it displays the "file2.el" buffer.

  3. I use eval-expression an expression (display-buffer "file1.el"), it opens a new window that displays file1.el buffer on the same frame.

  4. I want to kill the file1.el buffer and its corresponding window, so I eval-expression an expression (quit-window t (get-buffer-window "file1.el")), and I got what I want.

However, things are different when I do some buffer switches on the window displaying "file1.el", just after step 3. If I have done it, the step 4 will only kill the "file1.el" buffer, but NOT quitting the window that displaying it. Instead, after step 4, that window will display another buffer that I've switch to previously.

My question is that why quit-window behavior is different in this scenario?

  • 2
    quit-window uses quit-restore-window to decide what to do: see their doc strings with C-h f quit-window and C-h f quit-restore-window.
    – NickD
    Commented Dec 23, 2022 at 15:38
  • Wht do you mean by window - a window as used by the operating system which emacs calls a frame - or an emacs window which is in a frame.
    – mmmmmm
    Commented Dec 23, 2022 at 16:44
  • 1
    – Drew
    Commented Dec 23, 2022 at 18:24
  • @Drew Thanks for pointing out that.
    – absuu
    Commented Dec 24, 2022 at 8:13

1 Answer 1


Thanks @NickD for the hint. The reason why quit-window does not quit the window is that, each window itself maintains a history of the buffers it has displayed. And the implementation of quit-restore-window makes use of it, from "(elisp) Quitting Windows":

If prev-buffer is still live when quitting window, quitting the window may reuse window to display prev-buffer

  • Could you, by any chance, figure out a sane way to ignore that, and just get rid of the window regardless (even if prev-buffer would then have no window, or (if it implies that) would need to get reassigned to another)?
    – Sz.
    Commented Aug 22, 2023 at 16:04

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.