Split Window in Emacs and Resize and Recenter Frame to Make Room

I got an email by reader Andrej this week, asking about ways to (ab)use frame recentering in Emacs so that when a split window is created, the frame would grow to make room.

So if you have a frame with 1000px width, and split the window to the right, it should enlargen to 2000px width. And then recenter.

The code turned out to be surprisingly easy. Maybe I’m just getting accustomed to all this Emacs stuff. (That’s happy news, in case you weren’t sure!!1)

(defun my/split-right-and-resize-frame ()
  (interactive)
  (split-window-right)
  (my/frame-double-width)
  (my/frame-recenter))

(defun my/frame-double-width (&optional frame)
  (let* ((current_width (frame-pixel-width frame))
         (double_width (* 2 current_width)))
    (set-frame-width frame double_width nil t)))

(global-set-key [remap split-window-right] #'my/split-right-and-resize-frame)
  • my/frame-recenter is taken from my post on frame centering
  • set-frame-width accepts pixel width when you set the 4th parameter to t

Looks like what it says on the tin:

Before and after the split

To limit the doubling to your monitor’s size:

(defun my/frame-double-width (&optional frame)
  (let* ((current_width (frame-pixel-width frame))
         (monitor_width (nth 2 (frame-monitor-workarea frame)))
         (double_width (min monitor_width
                          (* 2 current_width))))
    (set-frame-width frame double_width nil t)))

Receive new .