2010-01-14 56 views
17

在正常模式下(在VIM)如果光标在一个数,击中Ctrl键 - 递增1的数量现在我想要做的同样的事情,但从命令行。具体而言,我想要去的某些行,其第一个字符是数字,并加一,即,我想运行下面的命令:在Vim命令行使用CTRL-A递增一个号码

:g/searchString/ Ctrl-A 

我试图存储按Ctrl - 一个在宏(比如a),并使用:g/searchString/ @a,但我得到一个错误:

E492: Not an editor command ^A

有什么建议?

回答

22

你必须使用normal执行正常模式命令模式命令:

:g/searchString/ normal ^A 

请注意,您必须按Ctrl键 - V按Ctrl - 一个到获得^A字符。

+0

使用vim多年来一直和从未碰到“正常” - 库尔 – 2010-01-14 04:52:04

+0

@詹姆斯:未知:)的Vim的美容惊喜升ike没有其他软件! – 2010-01-14 08:59:28

+1

尝试vim问题的黑暗角落来发现更多未知:http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you - 关于 – idbrii 2011-02-23 19:17:24

0

我相信你可以在命令行上用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 
9

还有:g//normal把戏CMS发布,如果你需要的不仅仅是在该行的开始找到了一些更复杂的搜索要做到这一点,你可以这样做这样的:

:%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 
) 
+1

非常有帮助!我也喜欢你如何解释它。谢谢。 – romar 2013-03-28 08:45:52