I would like to be able to switch to an open window by typing the name of the buffer which is displayed in it. It would be something like C-x b which opens the selected buffer only if it is not opened in another windows and in that case the focus switch to that window.
3 Answers
For a slightly different approach, you can install the https://github.com/abo-abo/ace-window package (available from melpa) and bind ace-window
to some key sequence. When you invoke it, it gives each window a letter or number, which you can type to select. If you turn on ace-window-display-mode
this letter is also added to the mode line so you can see it in advance.
The answer to Defining the window pointed by "other-window" has a screencast showing it in action.
The package has extra features, for example if invoked with one universal argument it swaps the current window with the target window.
The https://github.com/abo-abo/ace-window/wiki has an extended example, with scrolling other windows, winner-mode
to save and restore window configurations and so on.
You can use interactive
to select a buffer, get-buffer-window
to find the window showing that buffer, and select-window
to switch to that window:
(defun open-window-by-buffer (buffer)
(interactive "bBuffer: ")
(select-window (get-buffer-window buffer) nil))
I love ido-mode
for selecting things like open buffers, so here's a solution using that:
(defun open-window-by-buffer ()
(interactive)
(let ((buffer
(ido-completing-read
"Buffer: "
(mapcar 'buffer-name (buffer-list)))))
(select-window (get-buffer-window buffer) nil)))
If you use Icicles then you can use command icicle-select-window
, bound to C-0 C-x o
to trip among windows using
their buffer names.
More generally, C-x o
trips among windows or frames, as follows (C-h k C-x o
):
C-x o
runs the commandicicle-other-window-or-frame
which is an interactive compiled Lisp function inicicles-cmd1.el
.
(icicle-other-window-or-frame ARG)
Select a window or frame, by name or by order.
This command combines Emacs commands
other-window
andother-frame
, together with Icicles commandsicicle-select-window
,icicle-select-frame
, andicicle-choose-window-for-buffer-display
.Use the prefix argument to choose the behavior, as follows:
With no prefix arg or a non-zero numeric prefix arg: If the selected frame has multiple windows, then this is
other-window
. Otherwise, it isother-frame
.With a zero prefix arg (e.g.
C-0
): If the selected frame has multiple windows, then this isicicle-select-window
with windows in the frame as candidates. Otherwise (single-window frame), this isicicle-select-frame
.With plain
C-u
: If the selected frame has multiple windows, then this isicicle-select-window
with windows from all visible frames as candidates. Otherwise, this isicicle-select-frame
.With plain
C-u C-u
: Same asicicle-select-window
with a negative prefix arg: Select a window from any frame, including iconified and invisible frames.With plain
C-u C-u C-u
: This isicicle-choose-window-for-buffer-display
, with windows from all frames (i.e., iconified and invisible) frames as candidates.If you use library
oneonone.el
with a standalone minibuffer frame, and if option1on1-remap-other-frame-command-flag
is non-nil, then frame selection can include the standalone minibuffer frame.By default, Icicle mode remaps all key sequences that are normally bound to
other-window
toicicle-other-window-or-frame
. If you do not want this remapping, then customize optionicicle-top-level-key-bindings
.
(select-window (get-buffer-window "NAME-OF-BUFFER"))
and/or the optional second argument ALL-FRAMES. Perhaps consider adding a test for whether the window exists (i.e., live) so as to avoid throwing an error if you make a typographical error in the buffer-name.