vim regex increment all numbers by 1

谁说胖子不能爱 提交于 2019-12-20 08:59:19

问题


I have a prepared statement in java that i am adding an argument to the front of. Long story short, I have to take a ton of set methods and increment their first argument by 1.

I'd like a quick way to do a search and replace matching all numbers, and then increment them by one.


回答1:


Figured it out.

%s/\d\+/\=(submatch(0)+1)/g

http://vim.wikia.com/wiki/Using_an_expression_in_substitute_command




回答2:


The only regex you need to know is \d.

:g/\d/exe "normal! \<C-A>"



回答3:


I mis-interpreted the question as to be asking how to increments a number by one on each line, i.e.

var1
var1
var1

to be

var1
var2
var3

So I thought I'd post an answer for that. The link @user606723 provided shows that you can do that with (say between lines 1 and 3):

:let counter=0|1,3g//let counter=counter+1|s/^/\=counter."\t"


来源:https://stackoverflow.com/questions/10420797/vim-regex-increment-all-numbers-by-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!