I installed linum relative with this. But installing this replaced my fixed linum. I want to have both fixed line number and relative line number as well. How can I do this?


The following links linum wiki and stackoverflow similar issue help me to reach a solution with the following code :

(require 'linum) 
(add-hook 'prog-mode-hook 'linum-mode)
(defvar my-linum-format-string "%3d")

(add-hook 'linum-before-numbering-hook 'my-linum-get-format-string)

(defun my-linum-get-format-string ()
  (let* ((width (1+ (length (number-to-string
                             (count-lines (point-min) (point-max))))))
         (format (concat "%d (%" (number-to-string width) "d)")))
    (setq my-linum-format-string format)))

(defvar my-linum-current-line-number 0)

(setq linum-format 'my-linum-relative-line-numbers)

(defun my-linum-relative-line-numbers (line-number)
  (let ((offset (- line-number my-linum-current-line-number))
        (current (line-number-at-pos)))
    (propertize (format my-linum-format-string current offset) 'face 'linum)))

(defadvice linum-update (around my-linum-update)
  (let ((my-linum-current-line-number (line-number-at-pos)))
(ad-activate 'linum-update)

It will show in the right column the absolute number of the line and in parenthesis the relative number in regard to the current line.


izkons's comment above seems like an answer to me...

I just use https://github.com/scottjad/linum-relativenumber which does it all. – izkon

Some caveat's about this solution:

  • This modifies linum settings so might not respect customizations
  • This might require a restart or a bunch of fiddling to switch off disable relative line numbers while keeping absolute line numbers.
  • There's no need to restart or fiddle to turn it off. Just type M-x linum-mode again.
    – izkon
    Nov 5 '17 at 16:11
  • I meant switch off just the relative line numbers (I've updated the answer accordingly).
    – Att Righ
    Nov 5 '17 at 17:19

Do you want to have them both running? Toggle?

You can try this package https://github.com/coldnew/linum-relative

And then I think you can toggle it

M-x linum-relative-toggle
  • In my question I have mentioned about the package, you have given. I want to have both of them running and shown on emacs. Jul 17 '16 at 6:27
  • Hmm those downvotes strike me as a little unfair! So I'll give you some moral support! The general question of "easily see both absolute and relative line numbers" feels related enough to this question that this is a useful answer.
    – Att Righ
    Nov 5 '17 at 15:16

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.