Incrementing Numbers on Non-consecutive Lines

less than 1 minute read

In my work projects, after each SQL statement we have a checkpoint statement that logs where in the procedure the run currently is. This is useful for debugging stored procedures, and also for performance tuning. Each checkpoint contains a reference number, the and how many rows were acted upon.

Keeping the reference numbers up to date can be a real pain. Some of the stored procedures are several thousand lines long and can contain 30-40 checkpoints. Updating each one when adding a statement toward the top can easily add 30 to 45 minutes to a change.

Why not let Vim update them automatically?

:let g:I=1
:%g/Checkpoint: \d\+/ s/\d\+/\=g:I/|let g:I=g:I+1
:%s/\(Checkpoint:\) \(\d\) /\1 0\2 /g

The second line handle the renumbering of all checkpoints based on their position and the third changes all single digit checkpoints to be 0 prefixed. This helps keep them in line when querying.

There’s probably a simpler way to do this, but for now, this works.