My idea was kind of this (but I'm open for other suggestions):

I need

  1. a functions to get names of windows (not frames)
  2. a way to read the current window configuration. e.g. (vertically-split a (horizontally-split b c))
  3. a functions to transpose 2 windows / modify window configuration. e.g. (vertically-split a (horizontally-split b c)) -> (vertically-split (horizontally-split b c) a) -> (vertically-split (horizontally-split c b) a)

The ace-swap-window command in the ace-window package does exactly this. If you install that package, the command C-u M-x ace-window will swap the contents of the current window with the one you select. If you do that a lot you'll want to bind it to something more convenient.

I don't understand what your point #3 has to do with points 1 and 2. I use the following function to toggle back and forth between a 2-window horizontal configuration and a 2-window vertical configuration.

(defun toggle-window-split ()
  (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)))))
              (if (= (car this-win-edges)
                     (car (window-edges (next-window))))
        (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))))))
| improve this answer | |
  • Interesting! but not what i was loking for. but it will be usefull some other time ;) or can you switch to the 'next' (without choosing it) even with more than 2 windows? – BorstenHorst Jan 25 '17 at 22:27
  • Which window is next if you have more than two? If you can explain what you want, it can be done. With ace-window selecting a window is done with a single keypress, so it's very quick & flexible – Tyler Jan 26 '17 at 4:30

M-x package-install buffer-move


were exactly what i'm looking for. thanks @npostavs for pointing me to https://www.emacswiki.org/emacs/TransposeWindows

| improve this answer | |

Here, check this out.

(defun window-children (window)
  "Return the children of WINDOW."
  (let ((child (window-child window))
    (while child
      (push child children)
      (setq child (window-next-sibling child)))
    (nreverse children)))

(defun window-transpose-siblings (&optional n k)
  "Transpose the K-th parent with its N-th sibling.

K defaults to 0 and N to 1, i.e. transpose the `selected-window'
with its right (or below) sibling.

Interactively, K is 0 and N the prefix-arg.  Unless this argument
is negative, in which case K is minus the prefix-arg and N equals
1.  The command remembers the last set value of K across
different invocations."
   (let ((n (prefix-numeric-value current-prefix-arg))
         (k (get this-command 'k)))
     (when (<= n 0)
       (setq k (- n) n 1)
       (put this-command 'k k))
     (list n k)))
  (unless n (setq n 1))
  (unless k (setq k 0))
  (let* ((w0 (selected-window)))
    ;; Go up K window nodes.
    (dotimes (_ k)
      (setq w0 (window-parent w0)))
    ;; Figure out the window to transpose with and do it.
    (let* ((siblings (window-children (window-parent w0)))
           (w1 (nth (mod (+ n (cl-position w0 siblings))
                         (length siblings))
           (s0 (window-state-get w0))
           (s1 (window-state-get w1)))
      (unless (eq w0 w1)
        (window-state-put s1 w0)
        (window-state-put s0 w1))
| improve this answer | |

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.