This is what I put in my init.el
file to fix the problem. It's a hack, at some sizes the left margin is a bit off but so far this is the best solution I've found to this problem.
(defun adjust-left-margin-hook ()
(let ((new-margin (+ 1 text-scale-mode-amount)))
(setq left-margin-width (if (< new-margin 0) 0 new-margin))
(set-window-buffer nil (current-buffer))))
(add-hook 'linum-before-numbering-hook 'adjust-left-margin-hook)
(add-hook 'text-scale-mode-hook 'adjust-left-margin-hook)
The function adjust-left-margin-hook
performs the following commands:
- sets
new-margin
to 1 more than the currenttext-scale-mode-amount
- sets
left-margin-width
tonew-margin
(or 0 if it's below the minimum). You can play around with this variable to adjust based on your preferences but take note, thatleft-margin-width
must be a positive integer - redisplays the buffer so the margin width changes are applied
Then the two hooks are there for the function to be called whenever the left margin needs to be adjusted. That is, before the linum or nlinum packages start numbering the lines or whenever you change the scale of the text manually.
My Emacs version is 27.0.50, but it should work on earlier versions starting from 23 as well.