Relative Line Numbers In Emacs

大兔子大兔子 提交于 2019-11-27 12:29:23

问题


Does anyone know how if something like this Vim Relative Line Numbers exists for emacs? I use vimpulse, and man, that would be super handy to have! I know some lisp, so if it doesn't, I could try to make my own, if I got a point in the right direction.

Update: Thanks to the correct response, I came up with this, that will show 1 for the current line, and -1 for the previous line, for combining with vimpulse yanks and deletes.

Thanks a ton to all who helped! I know it is not exactly what Vim does, but what good is the Relative line numbers in vim that start at zero?? Silly vim.

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

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

(defun my-linum-relative-line-numbers (line-number)
  (let ((test2 (1+ (- line-number my-linum-current-line-number))))
    (propertize
     (number-to-string (cond ((<= test2 0) (1- test2))
                             ((> test2 0) test2)))
     '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)

回答1:


(2012-03-16: line numbers are now right-aligned, and displayed in the correct face.)

The problem here is that by the time a custom linum-format function is called, point has already been moved (by linum-update-window) to the line in question, so we can no longer use it to establish the difference between the two lines; it would just print a zero for every line.

There is a linum-before-numbering-hook, but this is run after point has been moved to the start of the buffer, so that's not useful for our purpose.

The following code solves the problem by using advice for linum-update to store the current line number, so that it will be available to the custom linum-format function.

To right-align the numbers I initially used a hard-coded format string of %3d on the basis that a single window showing more than 100 lines of code was not terribly likely. If you're a fan of follow-mode, however (or simply have multiple windows on the same buffer), that circumstance becomes exceedingly likely; so the code now calculates the number of columns required dynamically. The use of linum-before-numbering-hook makes this more efficient than the approach taken by the default dynamic linum format.

Note that if you comment out the add-hook, the faster non-dynamic approach is used.

(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 "%" (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)))
    (propertize (format my-linum-format-string 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)



回答2:


I just came across Scott Jaderholm's code for this, and remembered seeing this question, so I decided to post a link to the relevant lines in his .emacs.

Update: If you're using MELPA (and you should be!), just M-x package-install RET linum-relative.




回答3:


In Emacs 26.1, there's a built-in line number mode (display-line-numbers-mode). Enable it and set (setq display-line-numbers 'relative) to use relative line numbers.




回答4:


On a related note, if you're only looking to move to a specific line, ace-jump-mode provides a command ace-jump-line-mode that lets you jump to a specific line visible on screen. It uses letters rather than numbers for lines, however:




回答5:


Look at M-x linum-mode and the linum-format variable.

linum-format is a variable defined in `linum.el'.
Its value is dynamic

Documentation:

Format used to display line numbers.
Either a format string like "%7d", `dynamic' to adapt the width as needed, or a function that is called with a line number as its argument and should evaluate to a string to be shown on that line.
See also `linum-before-numbering-hook'.



来源:https://stackoverflow.com/questions/6874516/relative-line-numbers-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!