I am trying to learn VIM and regular expressions.
Got stuck when trying to add line numbers to a Heidenhain CNC file.
L X+0.000 Y+0.000 F100 M
C X+10.000 Y-15.000 F M
CP IA+60.000 F M
Now I want to add line numbers to every line that starts with a capital letter.
0 L X+0.000 Y+0.000 F100 M
1 C X+10.000 Y-15.000 F M
2 CP IA+60.000 F M
Here is what i thought would work:
:%s/\(^\)\([A-Z]*\)\2/\=line('.')-1 . ' '
%s - preform substitute on whole file
\(^\) - add "beginning" of the line to first "backreference group"
\([A-Z]*\) - add the rest of a valid line to secound "backreference group"
\2/\=line('.')-1 . ' ' - substitute the first backreference with line number and whitespace
This prints line number on all lines.
I cant figure out how to add the secound backreference as a condition. Like: only preform substitute if \3 exists.
Any help is appreciated.