r/vim • u/sonicLodge91 • 22d 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.
5
u/gumnos 21d ago
I love how powerful the
:gcommand can be. Especially since thecmdthat follows is pretty much anyexcommand (or series of commands) relative to that line, so you can do things liketo indent every paragraph that contains "pattern" or
to underline lines containing "pattern"
2
u/-romainl- The Patient Vimmer 21d ago
Also known as "the power of
:g".1
u/macroKnoll37 22d 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