7
votes

How can I modify the way emacs picks which buffer to show after closing a buffer?

When I have multiple columns showing the same buffer, and then open another file in one of the buffers and then close the newly opened buffer, it doesn't switch back to the previous buffer, but to another buffer.

I'll try to explain with an example:

  • Start with a new emacs at *scratch*
  • C-x 2 (split into two columns)
  • C-x C-f 1 (find file 1)
  • C-x o (switch to other frame)
  • C-x b 1 (find file 1)
  • C-x C-f 2 (find file 2)
  • C-x k (kill buffer)

Now it switches to scratch but I would like it to show 1 in both windows again, is it possible to make emacs behave this way?

3

3 Answers

3
votes

This may not be a direct answer to your question, but it might help.

Emacs manages its buffer list, including deciding which buffer gets displayed when you kill one (via kill-buffer). I haven't looked into how it's done, but the documentation is "out there". Lots of people have created custom buffer-stack management magic to change the way emacs does things, maybe some of them are based on bayesian analysis, or whatever. You can imagine the possibilities.

I've never looked into changing the way emacs manages its buffers. Instead I just bind other-window and switch-to-buffer to easy keystrokes (C-x o, C-x b) and I get really good at using them.

you could create a simple function for what you want: it should destroys all other windows, then split the window so that the current buffer is displayed in both. Luckily, emacs has functions that do exactly those things.

(defun cheeso-show-buffer-two-windows ()
  "Close all other windows; then split, and show the current
buffer in both windows."
  (interactive)
  (delete-other-windows)
  (split-window-vertically))

Bind that to a keystroke, and badda-bing, you're there. This is a vertical split - the windows are displayed in a vertical stack. If you want it horizontally split (the windows are side-by-side), then replace ... well, you know.

0
votes

This also doesn't quite help directly, but Winner mode might help you get where you want to get.

0
votes

Are you using tabbar-mode? I had the same problem and for me tabbar was the cause. Tabbar adds the function tabbar-buffer-kill-buffer-hook to kill-buffer-hook. You can remove it with (remove-hook 'kill-buffer-hook 'tabbar-buffer-kill-buffer-hook).

If you don't use tabbar try M-x describe-variable kill-buffer-hook. One of the functions in this list should be responsible for messing with your buffers.