If I change margin and evaluate the code, it will only change only in the new window, but I also want write it the have for the new buffer or even write it as a function. How do I activate it also for my current buffer, after I change the variable?

1 Answer 1


Maybe code like this might help?

(setq my-margin-left 10) ;; eval to change the left margin immediately.
(setq my-margin-right 5) ;; eval to change the right margin immediately.

(defun my-update-margins ()
  (set-window-margins (get-buffer-window) my-margin-left my-margin-right))

(add-hook 'window-configuration-change-hook 'my-update-margins)
(add-hook 'window-state-change-hook 'my-update-margins)
  • 1
    that is awesome, thank you so much, what is the "add-hook" for?
    – David
    Commented Mar 17, 2023 at 10:32
  • 1
    These add-hook are only used as a trigger for the margin update function. In this case the margin is a fixed value, so it seems that only window-state-change-hook is enough, or maybe there is a more suitable hook. In my case I am changing the margin by percentage, so I needed these two hooks to detect window-width changes by window splitting or frame resizing.
    – roomworoof
    Commented Mar 17, 2023 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.