在Vim命令行中增加一个数字

在普通模式下(在Vim中),如果光标在数字上,按Ctrl-A会将数字增加1.现在我想做同样的事情,但是从命令行。 具体来说,我想去某些行的第一个字符是数字,并增加它,即我想运行以下命令:

:g/searchString/ Ctrl-A

我尝试将Ctrl-A存储在一个宏(比如a )中,并使用:g/searchString/ @a ,但出现错误:

E492:不是编辑器命令^ A

有什么建议么?


您必须使用normal来在命令模式下执行正常模式命令:

:g/searchString/ normal ^A

请注意,您必须按Ctrl-VCtrl-A才能获得^A字符。


除了CMS发布的:g//normal技巧外,如果您需要使用更复杂的搜索来完成此操作,而不是在行首找到一个数字,则可以执行如下操作:

:%s/^prefix patternzsd+zepostfix pattern/=(submatch(0)+1)

作为解释:

:%s/X/Y            " Replace X with Y on all lines in a file
" Where X is a regexp:
^                  " Start of line (optional)
prefix pattern     " Exactly what it says: find this before the number
zs                " Make the match start here
d+               " One or more digits
ze                " Make the match end here
postfix pattern    " Something to check for after the number (optional)

" Y is:
=                 " Make the output the result of the following expression
(
    submatch(0)    " The complete match (which, because of zs and ze, is whatever was matched by d+)
    + 1            " Add one to the existing number
)

我相信你可以在命令行上用vim来做到这一点。 但是这里有一个替代方案,

$ cat file
one
2two
three

$ awk '/two/{x=substr($0,1,1);x++;$0=x substr($0,2)}1' file #search for "two" and increment
one
3two
three
链接地址: http://www.djcxy.com/p/49439.html

上一篇: A in Vim command line to increment a number

下一篇: class & function names highlighting in Vim