在正常模式下(在Vim中)如果光标在数字上,则按下Ctrl- A将数字增加1.现在我想做同样的事情,但是从命令行开始.具体来说,我想去某些第一个字符是数字的行,然后递增它,即我想运行以下命令:
:g/searchString/ Ctrl-A
Run Code Online (Sandbox Code Playgroud)
我试图存储Ctrl- A在一个宏(说a),并使用:g/searchString/ @a,但我收到一个错误:
E492:不是编辑器命令^ A.
有什么建议?
CMS*_*CMS 27
您必须使用在命令模式下normal执行正常模式命令:
:g/searchString/ normal ^A
Run Code Online (Sandbox Code Playgroud)
请注意,您必须按Ctrl- VCtrl- A才能获得^A角色.
DrA*_*rAl 11
除了:g//normalCMS发布的技巧之外,如果您需要通过更复杂的搜索来执行此操作,而不仅仅是在行的开头找到一个数字,您可以执行以下操作:
:%s/^prefix pattern\zs\d\+\zepostfix pattern/\=(submatch(0)+1)
Run Code Online (Sandbox Code Playgroud)
作为解释:
:%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
)
Run Code Online (Sandbox Code Playgroud)