VIM: :set number 中的零索引行号 [英] VIM: Zero-Indexed Line Numbers in :set number

查看:24
本文介绍了VIM: :set number 中的零索引行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VIM,可以很容易地显示任何给定文件的行号:

Using VIM, it is easy to display line numbers for any given file with:

:set number

然而,行编号是 1-indexed,这意味着编号从 1 开始.通常这正是我想要的,但是我用来编译代码的特定工具报告零索引行号 - 即行号从零开始.

However, the line numbering is 1-indexed, this means that the numbering starts at 1. Normally this is exactly what i want, however a particular tool I am using to compile my code reports zero-indexed line numbers - that is, line numbers start at zero.

如何更改vim的行号来补偿,以便查看vim缓冲区中的数字与工具提供的错误相对应,另外跳转到特定数字也与工具的数字相对应.

How do I change vim's line numbering to compensate so that viewing the numbers in the vim buffer corresponds to the errors provided by the tool, and in addition jumping to a particular number corresponds to that of the tool as well.

也就是说,如果工具告诉我第 98 行有错误,我想通过键入98G"而不是97G"跳转到该行,并且我想要该行(实际上是第 97 行)vim) 在行号列表中显示98".

That is, if the tool tells me that there is an error on line 98, I want to jump to that line by typing "98G", not "97G", and I want that line (which is really line 97 in vim) to display "98" in the line number list.

虽然我可以过滤工具的输出,虽然相当简单,但它并不是一项微不足道的任务,因为该工具还输出对应于每一行的十六进制值,具有相同的零索引形式,这是输出信息性消息也是如此,不仅仅是错误,我有很多这样的项目.

While I can filter the output of the tools, while fairly simple it is not a trivial task because the tool also outputs hex values that correspond to each line, which has the same zero-indexed form, and this is is output in informational messages as well, not just for errors, and I have many such projects.

推荐答案

我不认为这是可能的;然而,假设您的外部工具的输出只是文本,过滤输出以便编号为 1 索引以匹配 Vim 可能相当容易.你能给我们一个你的输出的例子吗?

I don't think this is possible; however, assuming the output of your external tool is just text, it would probably be fairly easy to filter the output such that the numbering is 1-indexed to match Vim. Can you give us an example of your output?

编辑

或者,如果您从 Vim 调用外部命令,您可以执行以下操作(基本上,添加一个空行,运行外部命令,然后删除该空行):

Alternatively, if you call the external command from Vim, you could do something like (basically, add a blank line, run the external command and then delete the blank line):

command! RunMyExternalProgramme call RunMyExternalProgramme()

func! RunMyExternalProgramme()
    " Save the old setting of makeprg
    let s:savedMakePrg = &makeprg

    " Save the screen layout
    let s:savedView = winsaveview()

    if config_file != ''
        " Put a blank line at the start of the file
        :1put! =''
        " Save
        write
        " Change makeprg and run it
        let &makeprg = '/path/to/programme -options etc'
        make
        " Delete the blank line
        1d
        " Save
        write
    endif

    " Restore the screen layout (optional)
    call winrestview(s:savedView)

    " Restore the old setting of 'makeprg'
    let &makeprg = s:savedMakePrg
endfunc

这篇关于VIM: :set number 中的零索引行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆