Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim — replace ‘foo’ by ‘bar’ on lines NOT starting with ‘character’

Tags:

regex

replace

vim

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?

like image 553
rberaldo Avatar asked Jan 22 '13 20:01

rberaldo


2 Answers

Try :vglobal:

:v/^%/s/foo/bar/g
like image 128
Jim Davis Avatar answered Nov 15 '22 08:11

Jim Davis


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

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

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

like image 34
Rohit Jain Avatar answered Nov 15 '22 06:11

Rohit Jain