I'm trying to write a simple function to divide the Emacs frame into four quadrants. Once I get the splitting to work, each window will have Dired for a different directory.

The splitting isn't giving the results I want, because I can't move point to the window I want to split.

Here's what I have for the window splitting:

(defun split-into-quadrants () "Manipulate windows" (interactive)
       (delete-other-windows)
       (split-window-vertically)
       (split-window-horizontally)
       (next-window)
       (next-window)
       (split-window-horizontally)
       (next-window)
       (next-window))

The splits work fine. next-window has no effect.

If I issue these commands interactively, Emacs does exactly what I want. But when I execute the function, point remains in the first window and I end up with three windows in the top half of the frame and one in the bottom half. What am I missing?

  • Most of the answers I generally see use some of the built in functions, however, you might be interested in creating your own display something left, display something right, display something below, display something above, and select whatever window you want. Here is a display left and right example function, which includes a select window: stackoverflow.com/a/21544307/2112489. Here is a below example, without the select window -- but you can add that if you want: stackoverflow.com/a/21591259/2112489 The split window is creating during the display functions. – lawlist Dec 1 '14 at 4:45
  • 1
    BT56: next-window doesn't select a window; it just returns the window object for the next window. You're not doing anything with the return values. – phils Dec 1 '14 at 7:44
  • Slightly OT, but still important: You seem to be using the terms "function" and "command" interchangeably. Please note that they are not the same. – itsjeyd Dec 1 '14 at 9:29
up vote 3 down vote accepted

You are on the right track, but you should use other-window instead of next-window to navigate to a specific window. Conveniently, other-window allows you to specify how many windows to skip, which means that you don't need to call it multiple times when navigating to a window that is multiple hops away:

(defun split-into-quadrants () "Manipulate windows" (interactive)
       (delete-other-windows)
       (split-window-vertically)
       (split-window-horizontally)
       (other-window 2)
       (split-window-horizontally)
       (other-window 2))

The functions split-window-horizontally and split-window-vertically return the new window created. You can use use the function select-window to select the newly created window, for example

(select-window (split-window-horizontally))

Here is one way (not necessarily the best way) to achieve what you want

(defun split-into-quadrants ()
  "Manipulate windows"
  (interactive)
  (delete-other-windows)
  (let ((current-window (selected-window))
        (new-window (split-window-vertically)))
    (split-window-horizontally)
    (select-window new-window)
    (split-window-horizontally)
    (select-window current-window)))

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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