for loop with substitution

Viewed 113

I want to pass for loop as substitute command range:

for i in range(x, y) | is/$/foo/ | endfor

but i'm hit with Trailing characters: foo/ | endfor

1 Answers

The error reported by Vim is a bit misleading with regard to the actual problem in your command.

The actual problem is that the variable i can't be used like that. The whole substitution must be concatenated with :help expr-.. and passed to :help :execute. In this case:

for i in range(x, y) | execute i .. 's/$/foo/' | endfor

If you don't do that, the i in is/$/foo/ is interpreted as a literal i so the command you are using is :help :isearch, not :help :s. Vim thus reports a misuse of :is and completely—and rightfully—ignores your original intent.

Then there is another issue, not related to the problem at hand: :help :s already works on a range so there is no real need for a loop:

execute x .. ',' .. y .. 's/$/foo/'
Related