14
votes

If an Emacs frame is split in several windows and a new buffer is opened, is there a possibility to tell Emacs in which window to open the buffer.

Especially, if one window contains a dired buffer and I want to tell Emacs in which of the other opened windows a new file shall be displayed.

If the Emacs frame is for example split like this:

____________________
|        |         |
|        |         |
|   A    |    B    |
|        |         |
|        |         |
--------------------
|        |         |
|   C    |    D    |
|        |         |
--------------------

Where A,B,C and D are the windows. If A contains a dired buffer, can I tell Emacs to open a new file in a new buffer in window D?

3

3 Answers

6
votes

Here's another answer --

You can do what you want using Icicles. In Icicle mode, C-x o, which is normally other-window, is icicle-other-window-or-frame. A prefix arg gives it several alternative behaviors, one of which (for Emacs 24 and later) is to choose a window that will be used by the next buffer-displaying operation (e.g., C-x C-f, C-x b).

That's what happens if you use a double plain prefix arg: C-u C-u C-x o. That invokes command icicle-choose-window-for-buffer-display, which you could of course also bind to any other key.

You choose the target window by name (using completion or cycling etc.).

All that command does is set variable icicle-next-window-for-display-buffer to the window that you choose. Icicles advises commands display-buffer, switch-to-buffer, and switch-to-buffer-other-window so that the next time they are called they use the window that is the value of icicle-next-window-for-display-buffer. (After that invocation they return to their normal behavior.)

This means too that if you have another method of choosing a window, which you prefer (e.g. cycling using one of the other methods mentioned here), then you could modify that method to set icicle-next-window-for-display-buffer to the chosen window, to get the same effect (in Icicle mode). IOW, it does matter how you choose the window; just set the variable value to it and the next buffer-displaying action will use it.

-1
votes

Others will probably give you answers that more directly respond to your question. My answer is to just move the focus to the window you want and then use C-x C-f or whatever to open the file or display the buffer you want in that selected window.

In order to do what you requested directly, you need anyway to use a key sequence that invokes the command you want. And if you want something general that works with different commands (e.g. find-file and switch-to-buffer) then you need to use a prefix key or repurpose the prefix arg. That is typically just as many key strokes as it is to switch to the window and then use the usual command.

Of course, with my suggestion you need a command to quickly switch to the window you want. There are various commands out there that let you cycle among windows or use completion to quickly pick a window by name or number.

So that's what I would suggest: switch to the window and then display whatever you want there, instead of trying to combine (a) designating the target window with (b) invoking the command that displays the buffer. IOW, just do it the old-school way: go to the window and then display there.

Of course, if you don't really want to end up in that newly displayed buffer, and you just want to keep the focus where you started, then with my suggestion you would need to cycle (e.g.) back to your starting window. It's not clear from your description whether this is your use case. If it is, then my solution is probably not that helpful.

But even in that case, it might still be useful to split things up: (a) a command to (only) designate the window to use for the following display-buffer command and (b) the display-buffer command. IOW, do essentially the same thing I suggested, but the first step would only designate the target window instead of switching to it, and the second step would use that designated window. (You would need to define (a) and (b), as a general mechanism.)

In this case too my suggestion differs probably from what others might suggest, in that it does not try to combine window choice with display command. They are coupled sequentially, but not combined in one command.

-1
votes

make the window that you would like to load the buffer in,
activated and press:

C-x b

and you will be prompted by:

Switch to buffer (default *Buffer List*):

press enter(ret) without writing anything to choose the default. and then you will find buffer list loaded in that window. then choose the one you like to load and you will find it loaded in the same window.