In Vim regular expression, I know it is possible to replace foo
by bar
on all lines starting with %
using
:g/^%/s/foo/bar/g
but I want to replace foo
by bar
on all lines NOT starting with %
. Is there a way to easily do so?
You can just negate the %
character using character class
: -
:g/^[^%]/s/foo/bar/g
[^%]
match any character except %
, at the start of the string.
The inverse of :g
is :g!
, so your example could be expressed:
:g!/^%/s/foo/bar/g
Note that :g!
is just another way of writing :v
(cf. Jim Davis' answer)