6

When I turn on linum-mode or nlinum-modeand 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

2
  • Even if you get an acceptable answer here, please consider filing an enhancement request with Emacs Dev: 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.
    – Drew
    Commented Feb 21, 2016 at 21:42
  • Try adding this to your init.el: (eval-after-load "linum" '(set-face-attribute 'linum nil :height 125)).
    – aadcg
    Commented May 12, 2019 at 16:03

1 Answer 1

2

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 current text-scale-mode-amount
  • sets left-margin-width to new-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, that left-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.

5
  • 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.
    – phils
    Commented 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).
    – Stefan
    Commented 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:
    – Nikaoto
    Commented Sep 20, 2019 at 6:18
  • @Stefan couldn't edit my comment. Here are the images I wanted to include: imgur.com/a/2dG0FYI
    – Nikaoto
    Commented 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.
    – Stefan
    Commented Sep 20, 2019 at 12:22

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.