Here is an example text file:
Section 4, 5
Section 6, 7
Section 8, 9
If the format of these lines needed to be changed while preserving some of their content–for example Section 4, Subsection 5b
–there is a solution. Enter the following command:
:%s/Section \(\d\), \(\d\)/Section \1, Subsection \2b/g
This uses capture groups to store the reference numbers in the find pattern, then uses backreferences \1
and \2
to reinsert them in the replace pattern.
Now the file has the desired format:
Section 4, Subsection 5b
Section 6, Subsection 7b
Section 8, Subsection 9b
This technique is especially useful with very long files, for example a CSV file or SQL script with many lines.