1

从文档中,: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}?此语法记录在哪里?

4

1 回答 1

2

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

: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-21T10:31:46.513 回答