Emacs 中的相对行号
有谁知道如果 emacs 存在这样的 Vim 相对行号 会怎么样?我使用 vimpulse,天哪,拥有它会非常方便!我知道一些口齿不清,所以如果不会,如果我在正确的方向上有一个观点,我可以尝试自己制作。
更新: 由于正确的响应,我想出了这个,它将显示当前行的 1,以及上一行的 -1,用于与 vimpulse 猛拉和删除相结合。
非常感谢所有提供帮助的人!我知道这不完全是 Vim 所做的,但是 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Emacs 26.1 中,有一个内置的行号模式
(display-line-numbers-mode)
。启用它并设置(setq display-line-numbers 'relative)
以使用相对行号。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.(2012-03-16:行号现在右对齐,并以正确的方式显示。)
这里的问题是,到了自定义
linum-format
函数被调用时,点已经被移动(通过linum-update-window
)到有问题的行,所以我们不能再用它来确定两条线之间的差异;它只会为每一行打印一个零。有一个
linum-before-numbering-hook
,但这是在点移动到缓冲区开头之后运行的,因此这对我们的目的没有用处。以下代码通过使用
linum-update
的建议存储当前行号来解决该问题,以便自定义linum-format
函数可以使用它。为了右对齐数字,我最初使用了
%3d
的硬编码格式字符串,因为单个窗口显示超过 100 行代码的可能性不太可能。但是,如果您是跟随模式的粉丝(或者只是在同一缓冲区上有多个窗口),那么这种情况极有可能发生;因此代码现在会动态计算所需的列数。使用linum-before-numbering-hook
比默认的dynamic
linum 格式所采用的方法更有效。请注意,如果您注释掉
add-hook
,则会使用更快的非动态方法。(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 (bylinum-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 customlinum-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 offollow-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 oflinum-before-numbering-hook
makes this more efficient than the approach taken by the defaultdynamic
linum format.Note that if you comment out the
add-hook
, the faster non-dynamic approach is used.我刚刚遇到了 Scott Jaderholm 的代码,并记得看到过这个问题,所以我决定发布一个链接到 他的 .emacs 中的相关行。
更新:如果您使用 MELPA(您应该使用!),只需 Mx软件包安装 RET linum-relative。
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.
相关说明,如果您只想移动到特定行,ace-jump- mode 提供了一个命令
ace-jump-line-mode
,可让您跳转到屏幕上可见的特定行。但是,它使用字母而不是数字来表示行: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:emacs 28,这对我有用
可能会有所帮助
之前我使用“linum-relative”包,它与“diff-hl”冲突:行号在版本控制的差异区域中不可见,本机行号工作正常!
emacs 28, this worked for me
might be helpful
Earlier I was using "linum-relative" package, which was conflicting with "diff-hl": line numbers were not visible in version controlled diff regions, native line numbers are working fine!.
查看 Mx
linum-mode
和linum-format
变量。Look at M-x
linum-mode
and thelinum-format
variable.