4

I list the current windows with

#+begin_src emacs-lisp :tangle yes
(window-list)
#+end_src

#+RESULTS:
| #<window 14 on 28.Windows.org> | #<window 3 on .emacs.d> |

Then I tried to operate one of them with

(split-window '#<window 14 on 28.Windows.org>)

but get the error:

Symbol’s value as variable is void: on

Tried alternatively

(split-window #<window 14 on 28.Windows.org>)
(split-window <window 14 on 28.Windows.org>)

Report the same error,

How could I reference a window by a designated name?

3
  • Please consider removing all of the extraneous Org stuff from the question. You don't need to show Org syntax to show Lisp code. Thx. – Drew Mar 17 '20 at 18:35
  • ... and in this case, it obfuscates the fact that (window-list) returns a list - on which you can apply functions like nth in order to select and operate on a particular element, e.g.: (split-window (nth 0 (window-list))) will happily split the first window in the list. – NickD Mar 17 '20 at 19:38
  • @NickD ty, amazing solution. – AbstProcDo Mar 18 '20 at 0:51
4

What Org emits here is a printed representation of an otherwise opaque window object. While you can serialize a window to a string, you cannot go back from the string to the window. You can recognize such data types by their printed representation looking like #<...>, see https://www.gnu.org/software/emacs/manual/html_node/elisp/Printed-Representation.html for further details.

This doesn't mean that you can't do meaningful things with windows though. For example the following works just fine:

#+begin_src emacs-lisp :tangle yes
(split-window (selected-window))
#+end_src

#+RESULTS:
: #<window 38>
2
  • 3
    Is Org relevant here? If not, how about removing it? – Drew Mar 17 '20 at 18:36
  • To expand a bit on what @wasamasa said, it may not be obvious that you can store that opaque object in a variable: (setq foo (selected-window)) (split-window foo) – Phil Hudson Mar 18 '20 at 11:08
2

If you use Icicles then you can use command icicle-select-window to switch to windows by name, using completion.

icicle-select-window is an interactive compiled Lisp function in icicles-cmd1.el.

(icicle-select-window)

Select window by its name.

With no prefix arg, candidate windows are those of the selected frame.

With a prefix arg:

  • Non-negative means windows of all visible frames are candidates.
  • Negative means windows of all frames are candidates (i.e., including iconified and invisible frames).

A window name is the name of its displayed buffer, but suffixed as needed by [NUMBER], to make the name unique. For example, if you have two windows showing buffer Help, one of the windows will be called *Help*[2] for use with this command.

0

Build on the icicle answer you can do that without including this big library with that function:

(defun MY/select-window-by-name (name)
  "Selects the window with buffer NAME"
  (select-window
   (car (seq-filter
     (lambda (window)
       (equal name (buffer-name (window-buffer window))))
     (window-list-1 nil 0 t)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.