2

Emacs doesn't show the line number if it is the last line and the line is empty. Apparently, this is by design, but in the mode line, it shows the line number.

Emacs line number problem

My line number configuration is like this:

(setq display-line-numbers-type 'visual) (global-display-line-numbers-mode -1) (require 'conf-mode) (dolist (mode '(org-mode-hook ; All the modes to have line numbers prog-mode-hook conf-mode-hook text-mode-hook dired-mode-hook Man-mode-hook)) (add-hook mode '(lambda () (display-line-numbers-mode 1)))) 

How can I get Emacs to display the line number (on the left-hand side with all the other line numbers) when I'm on the last, empty line?

6
  • 3
    It's by design because there is no "last empty line" -- I can't think of any software which would read a file with a final newline and treat it as an additional line. (Something might do it, but it would be weird.) If you have a file with three lines and each line ends with a newline character, pretty much everything is going to call that "three lines" rather than "four lines". Commented Sep 19, 2024 at 5:20
  • 1
    The mode line is forced to show the next number because it only shows one value, and so it needs a way to differentiate between the last line of the buffer and where the cursor is now -- it would be confusing, otherwise. You can think of the mode line as showing the potential line number, and the left edge as the actual number of lines in the buffer. Commented Sep 19, 2024 at 5:20
  • (Not that I know for sure that both features were implemented with the same interpretation -- display-line-numbers-mode came along decades after line-number-mode -- but I believe the above is very much the reasoning for how display-line-numbers-mode behaves.) Commented Sep 19, 2024 at 5:26
  • If I remember correctly, there is either a bug report or a discussion on the Emacs Devel mailing list between Eli Z. and myself from several years ago where he stated that the end of the buffer on a line all by itself (without a line number) is "a feature". In my tinkering with the C source code back then, I created a test for no man's land and use a custom indicator at that location -- however, that is not a part of the standard Emacs and was never incorporated. Maybe someday I will pick-up that project again ... Commented Sep 19, 2024 at 13:18
  • 1
    This is one of the weirder edge cases in computing. Nobody can really agree on it, but POSIX did weigh in on the matter. The POSIX definition of a line is a sequence of characters followed by a newline character. Thus the newline character does not separate lines of text, it terminates lines of text. If you add some characters after the final newline, many programs won’t count it as a line. Try running printf "foo\nbar" | wc -l, for example. Commented Sep 20, 2024 at 0:07

2 Answers 2

1

As noted in the previous answers and comments, this behavior in Emacs is expected.

I also found this information lacking, so I created a package called trailing-newline-indicator.

It is currently being evaluated for inclusion in MELPA. Although it hasn't been approved yet, you can still install it directly from the repository.

EDIT: The package is now available on MELPA: https://melpa.org/#/trailing-newline-indicator.

2
1

I previously submitted a bug report on this particular issue, and the author of the native line numbers (Eli Zaretskii) responded by stating that this behavior is "a feature".

https://lists.gnu.org/r/bug-gnu-emacs/2017-08/msg01039.html

From: Eli Z. Subject: bug#28247: Display native line number on last line of buffer (when empty line). Date: Sun, 27 Aug 2017 05:40:20 +0300 > Date: Sat, 26 Aug 2017 15:29:36 -0700 > From: Keith David B. <esq@...> > > Emacs does not display a native line number on the last line of the buffer > unless there is something there in addition to the cursor. This is a feature: "the last line" is not actually a line, there's no character there. 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.