16

Say you have two buffers open like so:

------------------------------------
            |                      |
  buffer 1  |        buffer 2      |       
            |                      |
------------------------------------

What's the fastest way to switch the buffers so that you get this:

------------------------------------
           buffer 1                |
                                   |
------------------------------------
            buffer 2               |
                                   |
------------------------------------
3

1 Answer 1

17

Here's a defun that would do what you are looking for:

(defun toggle-window-split ()
  (interactive)
  (if (= (count-windows) 2)
      (let* ((this-win-buffer (window-buffer))
             (next-win-buffer (window-buffer (next-window)))
             (this-win-edges (window-edges (selected-window)))
             (next-win-edges (window-edges (next-window)))
             (this-win-2nd (not (and (<= (car this-win-edges)
                                         (car next-win-edges))
                                     (<= (cadr this-win-edges)
                                         (cadr next-win-edges)))))
             (splitter
              (if (= (car this-win-edges)
                     (car (window-edges (next-window))))
                  'split-window-horizontally
                'split-window-vertically)))
        (delete-other-windows)
        (let ((first-win (selected-window)))
          (funcall splitter)
          (if this-win-2nd (other-window 1))
          (set-window-buffer (selected-window) this-win-buffer)
          (set-window-buffer (next-window) next-win-buffer)
          (select-window first-win)
          (if this-win-2nd (other-window 1))))))

(Shamelessly copied from Magnars .emacs.d)

Plus if you call it again, it will re-split your windows in the original vertical orientation.

3
  • It would be nice if this could preserve the relative size relationship of the two windows. I often keep an uneven "short" window on the bottom, and when switching back and forth, it would be helpful to preserve this relative size. Although a narrow vertical window is probably less useful.
    – b4hand
    Sep 26, 2014 at 16:57
  • 1
    I can't tell you how many times I use this. So helpful.
    – c-o-d
    Oct 22, 2014 at 16:29
  • only works if you have no more than 2 windows (if (= (count-windows) 2). Tranpose-frame support unlimited windows.
    – azzamsa
    Jun 3, 2020 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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