REGEXP in VIM
Hi!
I am trying to learn VIM and regular expressions. Got stuck when trying to add line numbers to a Heidenhain CNC file. Example: Before. Code:
;Not code After. Code:
;Not code Code:
:%s/\(^\)\([A-Z]*\)\2/\=line('.')-1 . ' ' \(^\) - 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. //John |
This is what i started with:
Code:
:%s/^/\=line('.')-1 . ' ' |
This is almost correct:
Code:
%g/^[A-Z]/s/^/\=line('.')-1 . ' ' Code:
;Not code |
I solved it:
Code:
let X=-1|%g/^[A-Z]/let X=X+1|s/^/\=X let X=-1 Declare variable X and initialize it to -1 |%g/^[A-Z] Pipe to new command and search entire file for lines that starts with a capital letter /let X=X+1 Increment the variable X when a match is found... |s/^/\=X ...and print the number at the beginning of that line |
All times are GMT -5. The time now is 09:00 PM. |