VIM范围行号寄存器

4

是否有一个寄存器或变量来保存当前选定范围的行号?

line(".") 可以用于当前行,但在视觉范围内是否有相应语法?

编辑

如下面所述,'<'> 寄存器保存了起始和结束行。

除使用上述方法外,我还编写了一个VimScript函数,可以使用这些行号并执行外部命令,将其包含如下:

function! Github(line1, line2)
    execute "!github -f " . expand("%") . " -l " . a:line1 . " -n " . a:line2
endfunction
com! -range Github call Github(<line1>, <line2>)

我完全不懂VimScript,但通过Google搜索得知上述函数接受一个范围。然后我将起始和结束行号用于执行外部脚本github,该脚本与Github API进行交互并/或基于git信息打开浏览器到Github页面。

1个回答

8
  • '< and '> respectively.

    So: line("'<") and line("'>") should be what you expect

  • Also,

    :'<,'>sort
    

    to sort the last visual selection

  • `< to jump to the start of the last visual selection (also see :he v_o)

  • Finally, if 'cpoptions' does not include *, you can use :* as a synonym for :'<,'>:

    :se cpoptions-=*
    :*sort
    

3
非常完美的回答。即使作为长期使用 Vim 的用户,我也学到了一些新东西(如 :* 命令,比 gv: 命令少一个按键)。 - Ingo Karkat
@IngoKarkat 谢谢!对我来说,老实说,这不是关于减少按键次数;而是关于记住一个好的工作集(小而灵活)和方便打字。:'<,'> 打起来很痛苦。我通常会用 gv: 代替(这样就不需要依赖 cpoptions 来使用 :*)。 - sehe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接