I noticed that whenever I narrow a region in emacs, the line numbering is relative to the narrowed region rather than being relative to the original file. Is there any way I can preserve the line numbering from the original buffer ?

  • 1
    If you don't get an answer, consider making an enhancement request: M-x report-emacs-bug is also for requests.
    – Drew
    Commented Jul 25, 2016 at 20:51
  • @Drew -- Unfortunately, Eli Z. will do nothing to enhance linum.el: debbugs.gnu.org/cgi/bugreport.cgi?bug=17787#8 Perhaps submitting a feature request directly to Stefan for nlinum.el would have better results if that library does not already provide the feature the original poster seeks.
    – lawlist
    Commented Oct 29, 2016 at 4:36
  • 1
    @lawlist: The question does not say that OP is using linum.el. I suggest reporting such an enhancement request for nlinum.el.
    – Drew
    Commented Oct 29, 2016 at 4:52

1 Answer 1


A little late, but here's what I have in my .emacs. It does change the numbering for all linum-mode enabled buffers, but that's what I generally want:

(require 'linum)

(defvar my-linum-base-line nil)
(defvar my-linum-format nil)

(add-hook 'linum-before-numbering-hook
          (lambda ()
                (goto-char (point-min))
                (setq my-linum-base-line (count-lines 1 (point)))
                (setq my-linum-format
                      (format "%%%dd"
                                (+ my-linum-base-line
                                   (count-lines (point)

(setq-default linum-format
              (lambda (line)
                (format my-linum-format
                        (+ line my-linum-base-line))))

There's probably many things wrong with this in regards to proper elisp use, but it seems to work for me :)

  • Note that if your version of Emacs >= 26.1, you can use (setq-default display-line-numbers-widen 't) (Source: emacs.stackexchange.com/a/48591/18064). Not that I have anything against this implementation, just mentioning that current versions of Emacs have it already implemented.
    – colelemonz
    Commented Jan 3, 2022 at 20:49

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.