I installed Emacs 25.2 on a new system and cannot get the desired behavior with killing buffers when an Emacs frame is split into multiple windows. For example, I have C-x 3 bound to a variation of split-window-horizontally that opens a second buffer different from the first in the newly created window. When I kill this second buffer in the new window, I have the same buffer displayed in both the windows. On Emacs 24.5, I get a third buffer displayed in the new window which is quite convenient. How can I get the same behavior in Emacs 25.2?

  • Good question. They changed the behavior a fair amount, so you will likely find similar (or the same) questions posted here and there. Dunno whether this exact question is a duplicate. – Drew Nov 15 '17 at 3:58

@brad-howes saved my sanity on a very similar question 40581:

I had the same issue with the newer Emacsen. I finally found a solution that seems to work. There is a configuration setting called switch-to-visible-buffer which default to t. Set it to nil via M-x customize-variable or with a (setq switch-to-visible-buffer nil) in your $HOME/.emacs file.

Here's the help for switch-to-visible-buffer:

switch-to-visible-buffer is a variable defined in ‘window.el’.
Its value is nil
Original value was t

If non-nil, allow switching to an already visible buffer.
If this variable is non-nil, ‘switch-to-prev-buffer’ and
‘switch-to-next-buffer’ may switch to an already visible buffer.
If this variable is nil, ‘switch-to-prev-buffer’ and
‘switch-to-next-buffer’ always try to avoid switching to a buffer
that is already visible in another window on the same frame.

You can customize this variable.

This variable was introduced, or its default value was changed, in
version 24.1 of Emacs.

So that's it: (setq switch-to-visible-buffer nil) in your $HOME/.emacs file.

And it saved my life. Brad deserves ALL the points, so go to 40581 and +1 his answer there.

| improve this answer | |
  • I am in your (and brad-howes') debt. Thanks a lot. – Sagar Jha Aug 23 '18 at 0:13

If you do not find any better solution the following kill-buffer-hook function changes the buffer of the selected window. It prefers buffers not visible in windows.

(defun T-change-buffer-in-selected-window ()
  "If the current buffer belongs to the selected window
assign another buffer to the selected window.
Prefer buffers not visible in windows. (See `other-buffer'.)
Afterwards set the old buffer as active."
  (when (eq (window-buffer) (current-buffer))
    (set-window-buffer (selected-window) (other-buffer))))

(add-hook 'kill-buffer-hook #'T-change-buffer-in-selected-window t)
| improve this answer | |
  • Hi Tobias, it did not solve my problem. This works if I am killing buffers one by one. But, consider a case, where multiple windows show the same buffer. If I kill that buffer, all those windows show only one or two unique buffers. On Emacs 24, all those windows will display unique buffers. – Sagar Jha Nov 29 '17 at 19:18

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.