In the Vim regex, I know that replacing foo with bar with all lines starting with % using
foo
bar
%
:g/^%/s/foo/bar/g
but I want to replace foo with bar on all NOT lines, starting with % . Is there any way to do this?
You can simply negate the % character class using the character class : -
character class
:g/^[^%]/s/foo/bar/g
[^%] match any character except % at the beginning of a line.
[^%]
Try :vglobal :
:vglobal
:v/^%/s/foo/bar/g
The inverse to :g is :g! , so your example can be expressed:
:g
:g!
:g!/^%/s/foo/bar/g
Please note that :g! is another way to write :v (see Jim Davis answer)
:v
try :g/^[^%]/s/foo/bar/g to match all lines not starting with%