4

I really need this but couldn't figure out how to do it. Let's say I open a file in a new buffer which has 200 line of code. Now I'm in the beginning of my file in line 1. And Mode line showing All(1,0). Which indicating the current line and column number. Now beside this I want to see my total line number in this file which is 200.

I know there's Another alternative, to press Alt + SHIFT + > keys and that will make emacs jump to the last line of the document or file. But that's sufferable, each time entering a newline gives total number of line increase.

That's pretty much annoying because after going to the end of file i lost my position where i was. And making my way through back takes a lot of time.

So, it'll be a life saver for me if anyone can help me here 😰 Here is an example of my neovim mode line, what i'm trying to do in my emacs mode too.

enter image description here

2
  • 1
    Emacs can remember previous buffer positions with markers: emacs.stackexchange.com/q/9908/2287 With regards to total lines, Emacs does mode-line data calculations in C much faster than can be done in Lisp and the C can take advantage of cached data. I sure hope it wouldn't be necessary to call (line-number-at-pos (point-max)) each time the mode-line gets updated: emacs.stackexchange.com/q/3821/2287 , but there is a workaround that has some edge cases ... Perhaps other forum participants have a better idea ...
    – lawlist
    Sep 4, 2022 at 5:26
  • Thanks @lawlist that's solve my question remembering position part 👍 But still unable to get the Mode-line part here. Sep 4, 2022 at 6:14

2 Answers 2

3

EDIT in response to the comment

Just paste the code on the same line before (vc-mode vc-mode) like in the screenshot below, where Emacs automatically created a new line for it (I have changed the count-lines to line-number-at-pos version here):

enter image description here

Again, don't forget to press C-c C-c in the customize buffer to apply the settings. The total line number will show up immediately in the mode line (like in the screenshot, the number 91 next to L17).

END EDIT

I have found all kinds of posts about this subject (see links below), they currently seem kind of irrelevant to me (for example, about the efficiency of determining the total number of lines, the answers might be outdated). So in summary, currently I would suggest as the simplest solution to simply use M-x customize-variable mode-line-format to add the following form after the mode-line-position (assuming you are using close to vanilla Emacs):

(:eval (number-to-string (count-lines (point-min) (point-max))))

Don't forget to apply the setting by pressing the apply button in the customize interface (or press C-c C-c there).

The last link below shows how to efficiently obtain the total line number, including benchmarks. However, I have found by using:

(benchmark 100 '(with-temp-buffer
                  (dotimes (i 10000)
                    (insert (format "%d\n" i)))
                  (count-lines (point-min) (point-max))))

that currently there is not much difference (count-lines uses line-number-at-pos).

https://stackoverflow.com/questions/8190277/how-do-i-display-the-total-number-of-lines-in-the-emacs-modeline Display max line in bottom line (nox) (aka mode-line) https://emacs.stackexchange.com/a/3822/26163

3
  • 1
    Looking at @Lindydancer's answer, I think, depending on what behavior you prefer, I would indeed prefer (line-number-at-pos (point-max)) over count-lines. Sep 4, 2022 at 7:27
  • brother i didn't get this. where to put the code? (:eval (number-to-string (count-lines (point-min) (point-max)))) I go to mode-line-position and there one thing call State where can i write. I there I suppose to put those code line ? NOTE: I'm very much noob. Sep 4, 2022 at 8:03
  • Thanks a lot 😍 that was life saver for me 😘. I got another problem, I wish if you could see, I'll get the solution here too. please check this one here : emacs.stackexchange.com/questions/73424/… Sep 4, 2022 at 15:15
4

You control the mode line using the variable mode-line-format. Typically, it is a list of entries that Emacs walk through recursively to decide what the mode line contains.

For example, the following adds a new entry which prints the line numbers as "CURRENT/LAST". They are currently placed last, and the old line number indicator is still present. You can probably come up with a better looking layout by rearranging things in the variable.

(defvar my-mode-line-total-lines
  '("%l/" (:eval (number-to-string (line-number-at-pos (point-max)))))
  "Mode line snippet for current and total number of lines in buffer.")
(put 'my-mode-line-total-lines 'risky-local-variable t)

(add-to-list 'mode-line-format 'my-mode-line-total-lines t)
3
  • hello Mr. @Lindydancer would please explain this little bit more. I mean where I suppose to put the code? Same as Mr. @ dalanicolai showed here in his answer or in my init.el file? Sep 4, 2022 at 15:25
  • You can drop the code right into your init file. However, it will simply add the CURRENT/LAST entry to the end of the mode line. You might want to disable line-number-mode and column-number-mode, add some whitespace etc. to make it more readable. Sep 4, 2022 at 17:09
  • I did put your full code in my init file. But that's not working or showing errors or anything new on Mode-line ! Sep 5, 2022 at 0:40

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.