Suresh Govindachar wrote:
Recently, Tim Chase wrote in the thread "replace
with a number sequence":
...8<---
> Things get a little trickier if one wants to do the replacement
> in a single line of multiple instances...
>
> opInstance(1), opInstance(2), opInstance(3)
>
> One has to write a function that effects a global variable:
>
> let g:start=0
> function! Incrementer()
> let l:result = g:start
> let g:start = g:start + 1
> return l:result
> endfunction
>
> and then do something like
>
> :%s/regexp/\=Incrementer()/g
>
> It would be nice not to have to use global variables for
> something like that.
Can't one initialize the unnamed register, @" and use a :g with
a command along the lines of 'replace with @" and increment @"'
(I tried some experiments to implement the above idea but could
not do so. But my inability to implement it does not meant that
it can't be done.)
--Suresh
I don't see any advantage (and I see some inconvenients) in using a register
rather than a variable; but you could do with a script variable (which could
be used globally but only via the functions defined in the script):
function SetStep(value)
let s:stepvar = a:value
endfunction
function GetStep(increment)
let s:stepvar += a:increment
return s:stepvar
endfunction
call SetStep(-1)
1,$s/OpInstance(\zs\n\+\ze)/\=GetStep(1)/g
...or a local variable to the buffer where you do the substitute (untested):
function GetStep(varname, increment)
exe "let" a:varname "+=" a:increment
exe "return" a:varname
endfunction
let b:counter = -1
1,$s/OpInstance(\zs\n\+\ze)/\=GetStep("b:counter", 1)/g
Best regards,
Tony.