4
votes

I would like to customize the behavior when I split windows in Emacs:

I am always splitting because I want to view a separate buffer side-by-side with the one I'm currently editing.

I use electric-buffer-list (bound to C-x C-b) to navigate buffers.

I end up doing all of the following separately:

  1. C-x 3 to split horizontally.

  2. C-x o to switch to the other window.

  3. C-x C-b to invoke electric-buffer-list so I can select the buffer I want to view.

It seems like I should be able to write an Elisp function that will do all of this when I press C-x 3.

I found this post which describes the focus switching part of the behavior that I want, but I don't understand how to extend that answer to achieve all of what I'm trying to do.


Edit: After reviewing @lawlist's post and debugging my syntax, I think I want to do something like this:

(defun split-right-and-buffer-list ()
  (interactive)
  (split-window-horizontally)
  (other-window 0)
  (electric-buffer-list 0))

(global-set-key (kbd "C-x 3") 'split-right-and-buffer-list)

This does everything I want, except that the buffer list that comes up only lists the current buffer, instead of the normal list of all buffers that I get when I invoke electric-buffer-list from its key binding.

2
Here is a similar thread where I modified split-window-below -- you can create your own function that splits horizontally: stackoverflow.com/a/19300409/2112489 You don't have to use a defalias -- i.e., you can just call your own revised function by name. When in the window you want, you can use switch-to-buffer -- to move between windows we use select-window . . .lawlist
How about something like either of these links, which displays the standard buffer-list in a split-window -- you can modify the examples to display a different kind of buffer-list if you so choose. This link is a left or right split example: stackoverflow.com/a/21544307/2112489 This link is a split-below example: stackoverflow.com/a/21591259/2112489lawlist

2 Answers

2
votes

With some very small modifications the function you came up with will do what you want:

(defun split-right-and-buffer-list ()
  (interactive)
  (split-window-horizontally)
  (other-window 1)
  (electric-buffer-list nil))

(global-set-key (kbd "C-x 3") 'split-right-and-buffer-list)
  1. Passing 1 instead of 0 as an argument to other-window causes Emacs to select the new window created as a result of calling split-window-horizontally.

  2. Passing nil instead of 0 as an argument to electric-buffer-list causes Emacs to show all buffers, not just file-visiting ones.

    The thing that can trip you up here is that this isn't mentioned in the documentation for electric-buffer-list (which doesn't include any information about the ARG it takes). But when you look at the source code of this command, you'll notice that it simply passes the value of the argument on to a function called list-buffers-noselect (and doesn't use it for anything else). The documentation of this function contains the missing piece of information mentioned above.

1
votes

If you do not mind having custom commands to do what you want try the following functions

(require 'ido)

(defun my-split-window-open-buffer-right (buffer)
  (interactive (list (ido-read-buffer "Please select a buffer: ")))
  (select-window (split-window-right))
  (switch-to-buffer buffer))

(defun my-split-window-open-buffer-below (buffer)
  (interactive (list (ido-read-buffer "Please select a buffer: ")))
  (select-window (split-window-below))
  (switch-to-buffer buffer))

Bind them to keys of you liking. I would prefer this over redefining/advising functions I have not written.