Skip to content

Instantly share code, notes, and snippets.

@tilgovi
Last active June 9, 2019 07:56
Show Gist options
  • Save tilgovi/9149797 to your computer and use it in GitHub Desktop.
Save tilgovi/9149797 to your computer and use it in GitHub Desktop.
Center Emacs Windows
(defun balance-margins ()
"This function balances the margins of all windows on the selected
frame such that the first column and the fill column are the same
distance from the left and right edge, respectively."
(walk-windows
(lambda (window)
(let* ((total-width (window-total-width window))
(fringe-width (apply '+ (butlast (window-fringes window))))
(scroll-bar-width (window-scroll-bar-width window))
(divider-width (window-right-divider-width window))
(extras-width (+ fringe-width scroll-bar-width divider-width))
(body-width (- total-width extras-width))
(fill-column (buffer-local-value 'fill-column (window-buffer window)))
(excess (max (- body-width fill-column) 0))
(margin (floor (/ (float excess) 2.0))))
(set-window-margins window margin margin)))))
;; Enable this hook to balance margins automatically when visiting files or adding/removing windows.
;; (add-hook 'window-configuration-change-hook 'balance-margins t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment