Tags: #vim #increment
So I had an interesting problem where I had a block of code like…
"testing0.testing-performance-domain.com" = {
val1 = "value_1"
...
}
The … is lots of key/value lines, so it ends up being a large ‘block’.
I needed to duplicate the block multiple times (200 in fact) but the first line needed an incremented number, so…
"testing1.testing-performance-domain.com" = {
val1 = "value_1"
...
}
"testing2.testing-performance-domain.com" = {
val1 = "value_1"
...
}
"testing3.testing-performance-domain.com" = {
val1 = "value_1"
...
}
The way I solved this was to:
"a
register contained 0
.V%y
(copy the block).199P
(duplicate it, as we need two hundred blocks).qq
(on the first line of the first block I record the following macro 0f0v"ap^AvTg"ay
).
0
go to start of the line.f0
find the first zero (e.g. the zero in testing0
).v"ap
select the zero and replace it with the contents of the "a
register (the first time round this would be the same number: 0
).^A
press Ctrl-a to increment the number pasted from the register.
vTg
select back to just before the g
in the preceding word “testing” (i.e. select the new number)."ay
and yank the new number into the same "a
register."testing0.testing-performance-domain.com" = {
):g/\v^\s\s\s\s"testing\d/norm @q
find all relevant blocks and run the macro.