6

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?

rberaldo
  • 207
  • 2
  • 8

4 Answers4

11

Try :vglobal:

:v/^%/s/foo/bar/g
Jim Davis
  • 5,241
  • 1
  • 26
  • 22
6

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

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

[^%] match any character except %, at the start of the string.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
6

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)

Michael Dunn
  • 8,163
  • 4
  • 37
  • 54
3

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

Gereon
  • 17,258
  • 4
  • 42
  • 73