Search and replace in vim in specific lines
Solution 1:
Vim has special regular expression atoms that match in certain lines, columns, etc.; you can use them (possibly in addition to the range) to limit the matches:
:5,12s/\(\%5l\|\%12l\)foo/bar/g
See :help /\%l
Solution 2:
You can do the substitution on line 5 and repeat it with minimal effort on line 12:
:5s/foo/bar
:12&
As pointed out by Ingo, :&
forgets your flags. Since you are using /g
, the correct command would be :&&
:
:5s/foo/bar/g
:12&&
See :help :&
and friends.
Solution 3:
You could always add a c
to the end. This will ask for confirmation for each and every match.
:5,12s/foo/bar/gc