How can I display the max line of a file in the bar at the bottom? So, how could it say "L1/L1300" instead of just "Top L1", if the respective happens to have 1300 lines?

  • 1
    See this answer from StackOverflow. In short, there's no "easy" way due to performance issues. Commented Aug 31, 2016 at 20:03

1 Answer 1


count-lines (also used by line-number-at-pos) is increasingly less efficient as the buffer grows in size/length, whereas (format-mode-line "%l") does not have those limitations -- however, there are other limitations. See the related thread entitled "A faster method to obtain line-number-at-pos in large buffers": A faster method to obtain `line-number-at-pos` in large buffers

Performing anything remotely significant in the mode-line can cause a slow down (in part) because the mode-line for more than one visible window may be updated during redisplay. The following example updates the total line numbers in only the selected-window (i.e., active window). See the related thread entitled "Show something in active mode line instead of all mode lines": Show something in active mode line instead of all mode lines

The mode-line for the inactive window uses the previously recorded value for the total number of lines if said value exists, otherwise calculate said value.

Add additional hooks as needed to call ml-update-all.

(defvar ml-selected-window nil)

(defvar ml-total-lines nil
  "Previously recorded total lines in a buffer -- used for inactive windows.")
(make-variable-buffer-local 'ml-total-lines)

(defun ml-record-selected-window ()
  (setq ml-selected-window (selected-window)))

(defun ml-update-all ()
  (force-mode-line-update t))

(add-hook 'post-command-hook 'ml-record-selected-window)

(add-hook 'buffer-list-update-hook 'ml-update-all)

(setq-default mode-line-format '(
  " %l | "
    (let ((win (selected-window)))
      (with-current-buffer (window-buffer win)
        (if (or (eq ml-selected-window win) (null ml-total-lines))
            (goto-char (point-max))
            (setq ml-total-lines (format-mode-line "%l")))
  • The (save-excursion ...) expression clocks in at 0.5ms on my laptop. So this should be sufficient performance wise. Leaves the question of how to elegantly incorporate this into the mode-line-format.
    – politza
    Commented Aug 31, 2016 at 23:08
  • @politza -- my preference would be to create a minimal working example that is as efficient as possible, without opening a "Pandora's box" by creating a complex mode-line-format or getting into the realm of unlimited possibilities with custom variations from the default -- e.g., the powerline library.
    – lawlist
    Commented Sep 1, 2016 at 5:48
  • I was simply hinting at the fact, that computing the value is relatively easy compared to getting it actually displayed in place of the default line-number. Which is, of course, not your fault.
    – politza
    Commented Sep 1, 2016 at 6:54

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.