6

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.

Nisba
  • 895
  • 8
  • 19
  • 3
    There 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 Answers3

2

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.

icarus
  • 1,904
  • 1
  • 10
  • 15
2

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)))
1

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 command icicle-other-window-or-frame which is an interactive compiled Lisp function in icicles-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 and other-frame, together with Icicles commands icicle-select-window, icicle-select-frame, and icicle-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 is other-frame.

  • With a zero prefix arg (e.g. C-0): If the selected frame has multiple windows, then this is icicle-select-window with windows in the frame as candidates. Otherwise (single-window frame), this is icicle-select-frame.

  • With plain C-u: If the selected frame has multiple windows, then this is icicle-select-window with windows from all visible frames as candidates. Otherwise, this is icicle-select-frame.

  • With plain C-u C-u: Same as icicle-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 is icicle-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 option 1on1-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 to icicle-other-window-or-frame. If you do not want this remapping, then customize option icicle-top-level-key-bindings.

Drew
  • 75,699
  • 9
  • 109
  • 225