When I turn on linum-mode
or nlinum-mode
and increase the font size via C-x C-+
it does not increase the size of the "margin" where the line number is displayed, so only part of the line number is visible. Is there a way to configure either of these modes so that the entire line number is always visible regardless of font size? Tested using emacs 24.5 on OS X and emacs 24.3.1 on ubuntu 14.04
1 Answer
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.
-
n.b. In Emacs 26+ it's highly recommended to use
display-line-numbers-mode
if possible, as it's dramatically more efficient than other line-numbering libraries.– philsCommented Sep 15, 2019 at 10:08 -
I neither understand why this works for you (it doesn't seem to pay attention to the width of the line-numbers themselves) nor why you need it (linum should obey the scaling automatically in recent Emacsen).– StefanCommented Sep 15, 2019 at 12:04
-
@Stefan the line numbers are scaling on their own. Better to show than tell. This is what the line numbers looked like before I applied my fix:– NikaotoCommented Sep 20, 2019 at 6:18
-
@Stefan couldn't edit my comment. Here are the images I wanted to include: imgur.com/a/2dG0FYI– NikaotoCommented Sep 20, 2019 at 6:25
-
@Nikaoto: By "obey the scaling" I mean that the margin should grow automatically to accomodate the numbers. So, if that doesn't work for you, I suggest you
M-x report-emacs-bug
.– StefanCommented Sep 20, 2019 at 12:22
M-x report-emacs-bug
. This behavior should probably be optional (e.g., add a user option), as some users might depend on or prefer the current behavior.(eval-after-load "linum" '(set-face-attribute 'linum nil :height 125))
.