繁体   English   中英

在Vim命令行中使用Ctrl-A增加一个数字

[英]Using Ctrl-A in Vim command line to increment a number

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

:g/searchString/ Ctrl-A

我试图在一个宏(比如说a )中存储Ctrl - A ,然后使用:g/searchString/ @a ,但是我收到一个错误:

E492:不是编辑器命令^ A.

有什么建议?

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

:g/searchString/ normal ^A

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

除了由CMS发布的:g//normal技巧之外,如果您需要通过更复杂的搜索来执行此操作,而不仅仅是在行的开头找到一个数字,您可以执行以下操作:

:%s/^prefix pattern\zs\d\+\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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM