2

In a buffer in Emacs, a line is the text you get from the first character after you press "Enter" to the last character before the next "Enter". And so, this is the line numbers one gets to the left of the buffer when you are in the display-line-numbers-mode.

Is there any way to get not these line numbers, but the "number of line" you get by counting the number of times you press C-n when moving down the buffer from the beginning? Namely, counting the numbers of lines (as I see in the screen) from the beginning of the buffer till the current position, and showing them in the screen as display-line-numbers-mode does.

I have tried using M-q on the line, but this is not what I am looking for as this command truncates the line into multiple lines.

[Edited in order to make the question clearer.]

I'd like to get the kind of thing you get when (in the windowed version of Emacs) you activate "Options → Show/Hide → Line Numbers for All Lines → Visual Line Numbers". But I don't quite understand this option, because it's not counting the lines from the first one in the buffer. Instead, the number in front of each line changes depending on the line in which the cursor is.

[Edit 2]

Example: enter image description here

In this image I have two lines (I am using word wrapping). Well, the idea is that I'd like to show a number in each line I see, that is, from 1 (at the beginning) to 12 (at the end of the buffer). Of course, if I change the width of the window I'd get a different number of lines.

  • 1
    I don't understand what you mean. Maybe you can provide a short example of a buffer with a few lines and what it should look like? – NickD Oct 22 at 16:30
  • 1
    Like @NickD, I don't understand what you mean. But I have some inspired guess. You can toggle between lines showing with a virtual newline breaking at the end of your display to show the rest of the line in a new display line, rather than hiding it somewhere to the left or right of your monitor. Is it that what your question is about? – Gyro Gearloose Oct 22 at 16:56
  • 1
    @NickD, I edited the question trying to make it clearer. – Matematikisto Oct 22 at 17:02
  • When in dired-mode you can M-x dired-find-file-literally. This way you can see the characters "naked". This is a great way to see alien characters by eiy-sight; not direcly what you asked but maybe this helps. – Gyro Gearloose Oct 22 at 17:03
  • 1
    You basically want linum-mode for the visual lines in visual-line-mode. – Zeta Oct 23 at 17:01
2

There is no built-in functionality to provide line numbers based on visual lines. First of all, compared to buffer lines (e.g. \n, \r\n), visual lines need to get recounted on every window width-change, which can bring Emacs down to its knees. Compared to normal buffer lines, visual lines are only second-class citizens in that regard.

However, the old linum-mode provides a starting point to get your desired behaviour. We can change linum-mode in a way that counts visual line modes, but we will disregard some warnings.

Here are the necessary steps:

  1. Copy linum.el to another file. For this demonstration, let's call it vinum.el
  2. Open vinum.el and M-x replace-string RET linum RET vinum RET
  3. Replace forward-line with next-line (warning: next-line is only meant for interactive use, however, C-n is next-line and you want to use its behaviour)
  4. M-x eval-buffer
  5. Open a buffer with long lines and enable both visual-line-mode and vinum-mode.

This will still use the correct line number for the first line. If you always want to start the numbers by 1, then replace (line-number-at-pos) by 1. If you want to count all visual lines, then replace (window-start win) by (point-min) and (window-end win t) by (point-max) instead.

| improve this answer | |
  • This is working as I expected. Thanks! The only (minor) detail is that in order to refresh the line numbers (for example, if I keep writing or if I resize the window) is disabling and enabling again the vinum-mode. – Matematikisto Oct 25 at 16:40
  • 1
    @Matematikisto you can probably add some more hooks to also refresh on those changes, but the whole thing is rather wonky. Someone else might be able to create a proper minor-mode, but I don't have enough elisp experience yet. – Zeta Oct 25 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.