Better window splitting in Emacs


Anybody else ever found the default behaviour of Emacs' automatic window splitting (split-window-sensibly) jarring? Especially when coming from dynamic dynamic tiling X11 window managers like bspwm that default to splitting along the longest edge of focused windows.

Well, I do. Even more important since I ragequit from my work's macOS crappy window management and decided to use ghostel inside Emacs to say goodbye to Alt+Tab hell.

So, here's the addition I finally cooked up for my init.el:

(defun split-window-along-longest-side (&optional window)
  "Split WINDOW according to its pixel dimensions."
  (interactive)
  ;; cf split-window-preferred-function documentation, display-buffer is weird...
  (setf window (selected-window))
  (if (> (window-pixel-width window) (window-pixel-height window))
      (and (window-splittable-p window t)
           (split-window-right))
      (and (window-splittable-p window nil)
           (split-window-below))))

(setq split-window-preferred-function #'split-window-along-longest-side)

(global-set-key (kbd "C-x RET") 'split-window-along-longest-side)

Together with custom, decreased values for split-width-threshold/split-height-threshold and that last keybinding (because my brain refuses to untangle C-x 2 from C-x 3), I finally have a decent WM inside Emacs.