0

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?

4

3 Answers 3

1

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-do-it))
(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.

0

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
3
  • In my question I have mentioned about the package, you have given. I want to have both of them running and shown on emacs. Commented Jul 17, 2016 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
    Commented Nov 5, 2017 at 15:16
  • The question links to linum-relative, but uses "this", which means you have to either parse the link, or follow it, to actually know which package they're talking about. Downvotes a tad unfair
    – Jacob Lee
    Commented Oct 6, 2022 at 18:29
0

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.
2
  • There's no need to restart or fiddle to turn it off. Just type M-x linum-mode again.
    – izkon
    Commented Nov 5, 2017 at 16:11
  • I meant switch off just the relative line numbers (I've updated the answer accordingly).
    – Att Righ
    Commented Nov 5, 2017 at 17:19

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.