I've got a somewhat obfuscated javascript file with multiple variables named a##########
, where #
stands for a single 0-9
digit. Each such identifier may or may not be unique.
I was substituting these manually, using :%s/<c-r>k/f#/g
, where k
was the register I put one of these identifiers and #
was a number I was incrementing manually.
This soon proved quite slow and tedious, as well as error-prone, so I decided to do them all at once, automatically. I tried the help files and online and came up with this (after I'd named the first 30 occurrences manually):
let: counter = 31 | g/a\d\{10\}/s//\="f".counter/ | let counter = counter + 1
This worked, except instead of running each substitution globally, it ran it for each match individually, thus distorting the meaning of the identifiers (e.g. where there would be a234789453
5 times, it would give me f1
, f2
etc. for each match, instead of replacing all instances of the same match with the same text).
So I ran the same, placing the regex in s
's call and using the g
flag for s
and got the same results, whether I made these modifications to the original command alone or separately. Then I tried:
let: counter = 31 | %s/a\d\{10\}/\="f".counter/ | let counter = counter + 1
Which resulted in all occurences of the pattern being replaced by f31
, regardless of the g
flag or not.
Clearly, the issue is that I need to run %s/__match__/\="f".counter/g
for each match specifically, but I can't seem to find a way to explain this to vim.
Is there a way to run a global substitute for each unique match of a regex?