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               |
                                   |
------------------------------------
Malabarba
  • 22,878
  • 6
  • 78
  • 163
c-o-d
  • 910
  • 9
  • 19

1 Answers1

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.

waymondo
  • 1,384
  • 11
  • 16
  • 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 '14 at 16:57
  • 1
    I can't tell you how many times I use this. So helpful. – c-o-d Oct 22 '14 at 16:29
  • only works if you have no more than 2 windows `(if (= (count-windows) 2)`. Tranpose-frame support unlimited windows. – azzamsa Jun 03 '20 at 09:19