Vim - replace the string "foo by" with lines that do not begin with a character

In the Vim regex, I know that replacing foo with bar with all lines starting with % using

 :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?

+6
source share
4 answers

You can simply negate the % character class using the character class : -

 :g/^[^%]/s/foo/bar/g 

[^%] match any character except % at the beginning of a line.

+6
source

Try :vglobal :

 :v/^%/s/foo/bar/g 
+10
source

The inverse to :g is :g! , so your example can be expressed:

 :g!/^%/s/foo/bar/g 

Please note that :g! is another way to write :v (see Jim Davis answer)

+5
source

try :g/^[^%]/s/foo/bar/g to match all lines not starting with%

+3
source

All Articles