Vim:用 n+1 替换 n [英] Vim: Replace n with n+1

查看:50
本文介绍了Vim:用 n+1 替换 n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I replace every number n that matches a certain pattern with n+1? E.g. I want to replace all numbers in a line that are in brackets with the value+1.

1 2 <3> 4 <5> 6 7 <8> <9> <10> 11 12

应该变成

1 2 <4> 4 <6> 6 7 <9> <10> <11> 11 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

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

If you only want to replace in specific line, move your cursor on that line, and execute

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

或使用

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

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

(replace LINENUM with the actual line number, eg. 13)

这篇关于Vim:用 n+1 替换 n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆