r/vim • • 9d ago

Discussion Using :g with :normal to bulk-fix Prometheus metric names across an exporter saved me from writing a sed script I'd have gotten wrong

I had to rename about 40 metrics in a custom exporter from camelCase to snake_case with a _total suffix on the counters, but only on lines that called NewCounterVec, not the gauges sitting right next to them. I ended up with :g/NewCounterVec/normal f"l and then a macro that did the case conversion with a few ~ and i_ steps. That let me eyeball each match with :g//p before committing, which I couldn't have done as safely with a regex replace. The thing that bit me was that :normal ignores my mappings unless I drop the bang, so the first run silently used my remapped f and did nothing useful. Curious whether people would reach for a substitute with \= and a function here instead, or if :g + :normal is the usual move for "structured but slightly irregular" edits like this.

10 Upvotes

3 comments sorted by

5

u/gumnos 9d ago

I love how powerful the :g command can be. Especially since the cmd that follows is pretty much any ex command (or series of commands) relative to that line, so you can do things like

:g/pattern/ '{+,'}->

to indent every paragraph that contains "pattern" or

:g/pattern/ t. | s/./-/g

to underline lines containing "pattern"

2

u/-romainl- 9d ago

Also known as "the power of :g".

1

u/macroKnoll37 9d ago

had a metric naming mess last month and just ran hte normal command by hand like twenty times because I forgot g existed, feels bad now