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.
-
3There is no need for anything fancy, just `(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. – lawlist Nov 03 '16 at 17:06
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)))
- 33
- 3
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 oruns the commandicicle-other-window-or-framewhich 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-windowandother-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-windowwith 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-windowwith windows from all visible frames as candidates. Otherwise, this isicicle-select-frame.With plain
C-u C-u: Same asicicle-select-windowwith 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.elwith a standalone minibuffer frame, and if option1on1-remap-other-frame-command-flagis 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-windowtoicicle-other-window-or-frame. If you do not want this remapping, then customize optionicicle-top-level-key-bindings.
- 75,699
- 9
- 109
- 225