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?

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Something hacky here: http://stackoverflow.com/questions/11612364/emacs-relative-line-numbers-on-the-right-margin ... though perhaps you could use the ideas to customize relative-linum's display. – Joe Corneli Sep 11 '16 at 01:22
  • Maybe (better than the above) use the idea here: http://emacs.stackexchange.com/questions/21445/absolute-line-numbers-on-the-current-and-relative-line-numbers-on-all-other-line?rq=1 – Joe Corneli Sep 11 '16 at 01:27
  • I just use https://github.com/scottjad/linum-relativenumber which does it all. – izkon Sep 29 '17 at 16:32
  • This question seems pretty related: https://emacs.stackexchange.com/questions/36149/show-both-relative-and-absolute-line-numbers-in-emacs-26 – Att Righ Nov 05 '17 at 15:44

3 Answers3

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.

Brutus320
  • 51
  • 5
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
Pedro Luz
  • 154
  • 1
  • 5
  • In my question I have mentioned about the package, you have given. I want to have both of them running and shown on emacs. – Ansuman Bebarta Jul 17 '16 at 06: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 05 '17 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 Oct 06 '22 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.
Att Righ
  • 725
  • 4
  • 14