2025-11-24 Changing window layout

Sometimes I have my Emacs frame split, for example having two windows – one beside the other – and I want to preserve both windows but have them stacked on top of each other. Or vice versa.

I figured that it shouldn’t be too difficult to write some little function to do that (although dealing with windows and their positioning is notoriously complex – but also incredibly flexible). I decided to look for existing solutions first.

I was not disappointed.

It turns out that Emacs has me covered already! Today I learned about the window-x package, which is built into Emacs (and autoloaded, so you don’t even have to require it). The “commentary” at the top of the file is pretty short:
This file defines less frequently used window organization commands.

The package defines one user option (about which later) and several commands (most of which come in pairs). A word of warning: the commands have been renamed recently, so what you get depends on which Emacs version you have. So, unless you have a bleeding-edge Emacs (or are reading this in the future), your Emacs may have a slightly differently named set of commands than what I describe below. Say M-x find-library RET window-x RET to see what you have installed.

Edit: I received a message that I was wrong – this library is only available in Emacs 31, so if you use the official, released version, you’ll have to wait for them. Sorry for my mistake!

So, the first pair is window-layout-rotate-anticlockwise and window-layout-rotate-clockwise. It is not easy to describe their effect in words, but imagine something like rotating your screen by 90 degrees – so each horizontal window split becomes a vertical one and vice versa – but of course without rotating the windows’ contents.

Here is an example using screenshots, which might be helpful to wrap your head around my clumsy description. (Note that my Artist clock completely broke because its window was too short.) The original layout is on the left, and the result of calling window-layout-rotate-anticlockwise on the right.

window-layout-rotate-anticlockwise.png

The next pair is window-layout-flip-leftright and window-layout-flip-topdown. These are easy to explain – they just change the current window configuration to its mirror image (with a vertical or horizontal axis).

window-layout-flip-leftright.png

window-layout-flip-topdown.png

Then, window-layout-transpose is the only one that doesn’t have a pair. (Mathematically speaking, the pairs are inverses, and this one is its own inverse.) It just flips all the windows along a diagonal axis, so the top window(s) become the left one(s), the left one(s) move to the top, the one(s) on the right move to the bottom, etc.

window-layout-transpose.png

And the last two commands are rotate-windows and rotate-windows-back, which are (again) a bit tricky to explain in words. They leave the layout unchanged, but move the contents of the windows (clockwise or counterclockwise).

rotate-windows.png

The one user option comes into play here. It is called rotate-windows-change-selected and depending on its value, the selected window stays the same or moves with the content. (The default is t which means that the selected window changes contentwise – that is, another buffer is now in the selected window – but stays the same geometrically – that is, if the leftmost window was selected before rotate-windows, the leftmost window is selected afterwards, etc.)

If your head isn’t spinning yet, there’s more! Each of the commands in window-x supports a prefix argument. If called with C-u, they perform their machinations on the parent window of the current window instead of the whole frame.

I’m not entirely sure how useful all these functions are to me. I have bound window-layout-transpose to C-x 4 t (by saying (keymap-set ctl-x-4-map "t" #'window-layout-transpose) in my init.el). This covers my initial requirement of being able to switch between “two windows side by side” and “two windows on top of each other”. Time will tell if that is enough – if not, I could always create a dedicated window-x keymap at C-x 7 or C-x 9, which are currently unused, or even C-x 6 which is a prefix key for two column editing (a feature I’ve never used in over 2 decades of using Emacs!).

CategoryEnglish, CategoryBlog, CategoryEmacs