As part of the engineering responsibilities at my job I routinely have to edit configuration files on a particular tier of our infrastructure when building new systems, putting in IP addresses for particular feed entries. Unfortunately no one has enough time to automate this with a proper config generation tool so it’s something of a manual process at the current moment (hoping to change this soon with some Python when I get a moment).
A common task is to replace ‘a.b.c.d’ placeholders with the correct multicast addresses in a selection of lines but not on every single line. Obviously the ‘:s’ substitution command comes to mind with the ‘c’ flag to confirm, but when the file is hundreds of lines the fact substitution starts at the beginning of the file isn’t much help.
However, I sat down and looked into it a bit more and discovered a way to do do substitution with confirmation only on lines within a visual selection.
Make a visual selection with ‘v/V/^V’, then enter in a substitution command with the \%V prefix in your search regex:
:%s/\%Va\.b\.c\.d/1.2.3.4/gc
This will replace a.b.c.d with 1.2.3.4 within the selection and prompt you to confirm each match. I also noticed you’ll need to remove the automatically entered ‘<,’> characters when going into command mode with a selection highlighted.
Yay for Vim! Now only to get the Puppet infrastructure I started rolled out fully so I can get the vim-enhanced RPM installed on all these hundreds of boxes…