Reputation: 81
Let’s say I have multiple STRING
occurrences. I want to replace the 1st occurrence with STRING_A
, 2nd occurrence with STRING_B
, 3rd occurrence with STRING_C
.
e.g
Color of my pant is STRING. Color of my hair is STRING. Color of my car is STRING.
After I run search and replace, I should get:
Color of my pant is STRING_A. Color of my hair is STRING_B. Color of my car is STRING_C.
Any help will be greatly appreciated.
Upvotes: 2
Views: 91
Reputation: 172570
You can define a List of replacements, and then use :help sub-replace-expression
to pop replacements off it:
:let r = ['bar', 'baz', 'bak']
:%substitute/STRING/\=remove(r, 0)/g
Upvotes: 0
Reputation: 97948
From vim wiki:
let @a=1 | %s/STRING/\='STRING_'.(@a+setreg('a',@a+1))/g
But this will give you STRING_1, STRING_2 etc.
Slight modification gives the desired result:
let @a=65 | %s/STRING/\='STRING_'.nr2char(@a+setreg('a',@a+1))/g
If you want to get the substitutions from an array, first define an array:
:let foo=['bar','baz','bak']
Then do the substitution:
let @a=0 | %s/STRING/\=get(foo, @a+setreg('a',@a+1))/g
This will give you:
Color of my pant is bar. Color of my hair is baz. Color of my car is bak.
Upvotes: 2