【问题标题】:Vim's global command with patterns joined by comma带有逗号连接模式的 Vim 全局命令
【发布时间】:2019-03-25 01:34:34
【问题描述】:

从文档中,:global 命令的语法是:

:[range]g[lobal]/{pattern}/[cmd]
                        Execute the Ex command [cmd] (default ":p") on the
                        lines within [range] where {pattern} matches.

我也遇到过:g这样的用法:

:g/apples/+1,/peaches/ s/^/# /g
:g/start/+1,$ sort n

这里的/apples/+1,/peaches/ 属于{pattern} 吗?此语法记录在哪里?

【问题讨论】:

  • 请问您在哪里看到的这些用法?
  • @seuling:例如 Vim Tips Wiki。见herehere
  • 哦,谢谢!我知道了。很高兴你找到答案。无论如何,为什么我不能标记你的名字?
  • @seuling:在 Meta 上查看 answer

标签: vim command ex


【解决方案1】:

我刚刚在Vim Tips Wiki 中找到了对:global 的这种用法的解释:

:g/apples/,/peaches/ s/^/# /g
    Insert "# " at the start of each line in all identified blocks. 
    :g/apples/ identifies each line containing "apples". 
    In each such line, .,/peaches/ s/^/# /g is executed 
    (the . is assumed; it means the current line, where "apples" occurs). 

所以,/peaches/ 在这里定义了替换命令 的范围。有点令人困惑的部分(我在文档中没有提到)是初始 '.' 在一个范围内是可选的。添加它使命令更明显:

:g/apples/.,/peaches/s/^/# /g

【讨论】:

    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多