You can do it in a single command line but it's a bit involved if you're not fairly well-versed in Vim. (You could also record a macro to do it but I prefer this approach.)
:let idx=0 | g/img1\.gif/ let idx += 1 | s//\='img' . idx . '.gif'/
We have two commands here, separated by
|. The first,
let idx=0 is simple enough: it just initializes a counter variable to 0.
The rest is all one
g[lobal] command. This command searches for the pattern that is between the two
/ delimiters and for each match found the command or
|-separated commands that follow it will be executed. In this case we have two such commands...
The first simply increments our counter.
The second is the well-known substitution command but with a couple twists.
- We don't specify a pattern. That means we reuse the pattern that follows
- The replacement part uses what's known as a sub-replace expression. This allows us to use a dynamic replacement that includes our incrementing counter.
A sub-replace expression is denoted by starting the replacement section with
\=. Everything that follows that is evaluated as a Vim expression. The result of that evaluation becomes the replacement text. In this case our expression is the concatenation of two static strings and the counter variable:
'img' . idx . '.gif'. So if the counter is equal to 3 during an iteration the resulting replacement string is
If you'd like to learn more I have an answer that does a fairly deep dive into this useful feature.