Vim:用n + 1替换n

pfn*_*sel 8 vim replace

如何替换n与某个模式匹配的每个数字n+1?例如,我想用值+ 1替换括号中的行中的所有数字.

1 2 <3> 4 <5> 6 7 <8> <9> <10> 11 12
Run Code Online (Sandbox Code Playgroud)

应该成为

1 2 <4> 4 <6> 6 7 <9> <10> <11> 11 12
Run Code Online (Sandbox Code Playgroud)

cog*_*ita 12

%s/<\zs\d\+\ze>/\=(submatch(0)+1)/g

作为解释:

%s          " replace command
"""""
<           " prefix
\zs         " start of the match
\d\+        " match numbers
\ze         " end of the match
>           " suffix
"""""
\=          " replace the match part with the following expression
(
submatch(0) " the match part
+1          " add one
)
"""""
g           " replace all numbers, not only the first one
Run Code Online (Sandbox Code Playgroud)

编辑:如果您只想替换特定行,请将光标移动到该行,然后执行

s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
Run Code Online (Sandbox Code Playgroud)

或使用

LINENUMs/<\zs\d\+\ze>/\=(submatch(0)+1)/g
Run Code Online (Sandbox Code Playgroud)

(替换LINENUM为实际的行号,例如13)