replace - Vim substitute with ascending numbers inside a pattern -


i have pattern this:

word0word word0word word0word 

and need number ocurrences of pattern this:

word1word word2word word3word 

i found can this:

:let @a=1 | %s/word0/\='word'.(@a+setreg('a',@a+1))/g 

but omits rest of pattern (the second "word", need include identify pattern). there way include rest of pattern? tried:

:let @a=1 | %s/word0word/\='word'.(@a+setreg('a',@a+1))'word'/g 

but returns error. thought combining \zs , \ze, i'm not sure how approach that.

in second pattern you're missing . (to indicate concatenation).
work :

:let @a=1 | %s/word0word/\='word'.(@a+setreg('a',@a+1)).'word'/g                                           " 1 --^ 

note if want use capture groups in subreplace expression you'll need use submatch(x) instead of \x (replace x number of match, starting @ 1) :

:let @a=1 | %s/\vword0(\w+)/\='word'.(@a+setreg('a',@a+1)).submatch(1)/g 

see :h sub-replace-\= more informations on using subreplace expressions.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -