19

Is it possible to display relative line numbers on the right margin of an Emacs buffer, and keep normal line numbers on the left margin?

Mike
  • 19,267
  • 11
  • 56
  • 72
  • 4
    For those unfamiliar with the feature: [Relative line numbers in VIM](http://jeffkreeftmeijer.com/2012/relative-line-numbers-in-vim-for-super-fast-movement/); – dotancohen Jul 23 '12 at 12:20
  • I never heard emacs to have relative line numbers. – alinsoar Jul 23 '12 at 12:51
  • 1
    http://stackoverflow.com/questions/6874516/relative-line-numbers-in-emacs Just can't get 'em on the right side of the screen. – Mike Jul 23 '12 at 12:56
  • 2
    Yes, you could certainly do this, although you'll probably need a custom version of `linum.el` to do it (or redefine some of its functions at minimum), as the assumption of only working in the left-margin is very much hard-coded. Making it use the right-margin instead of the left is a fairly trivial change, but writing different values in each margin will certainly require a little more work. – phils Jul 23 '12 at 13:40
  • 1
    This isn't an answer to your question, but I would advise you to turn off line numbers. It really won't take long to get used to, and you'll save valuable horizontal space. `M-g M-g` will take you to any line you want to go to, and if you compile or run your programs from `compile` you can move through error messages without explicitly entering line numbers either. – Dale Hagglund Oct 21 '12 at 05:37
  • If only for quick jump in the buffer, i highly recommand Ace Jump. http://www.emacswiki.org/emacs/AceJump http://www.youtube.com/watch?v=UZkpmegySnc – whunmr Dec 05 '12 at 12:03

1 Answers1

9

This can be a starting point. Load this definitions and do M-x linum-mode.

(defun linum-relative-right-set-margin ()
  "Make width of right margin the same as left margin"
  (let* ((win (get-buffer-window))
     (width (car (window-margins win))))
    (set-window-margins win width width)))

(defadvice linum-update-current (after linum-left-right-update activate)
  "Advice to run right margin update"
  (linum-relative-right-set-margin)
  (linum-relative-right-update (line-number-at-pos)))

(defadvice linum-delete-overlays (after linum-relative-right-delete activate)
  "Set margins width to 0"
  (set-window-margins (get-buffer-window) 0 0))

(defun linum-relative-right-update (line)
  "Put relative numbers to the right margin"
  (dolist (ov (overlays-in (window-start) (window-end)))
    (let ((str (overlay-get ov 'linum-str)))
      (if str
      (let ((nstr (number-to-string
               (abs (- (string-to-number str) line)))))
        ;; copy string properties
        (set-text-properties 0 (length nstr) (text-properties-at 0 str) nstr)
        (overlay-put ov 'after-string
             (propertize " " 'display `((margin right-margin) ,nstr))))))))

See the screenshot

emacs screenshot

slitvinov
  • 5,693
  • 20
  • 31
  • This looks like it fits the bill nicely. Well worth the wait! Well done! – Mike Dec 28 '12 at 17:23
  • This is nice. However, I want the absolute linum to be displayed on the right and relative linum on the left (so I can have real line number together with linum-relative plugin from ELPA). How can I do it? – Amumu Dec 11 '13 at 06:13
  • both left would be better imo – 0x6C38 Aug 10 '17 at 22:32
  • I am getting following debug error: https://gist.github.com/avatar-lavventura/47792d4e5316a0b530c9ef2be1060aca – alper Aug 17 '20 at 11:42
  • @alper which version of emacs you use? I've just tried it with 26.1 and it worked. – slitvinov Aug 17 '20 at 14:10
  • ah the error was related to something else. But the line numbers are not the same numbers that are showed on the left margin. like on the bottom of the page last line has line number of 0 – alper Aug 18 '20 at 07:15