Blogs Directory

Saturday, June 8, 2013

VIM : Delete all the lines containing the pattern specified



VIM has a very useful command "g" which can be used to perform operations over the lines which contains the matched pattern.

The Syntax is :

:g/pattern/d

The above command deletes all the lines containing the pattern.

If you want to delete all the lines not containing the pattern then use the command as :

:g!/pattern/d

Note that the "g!" is equivalent to "v" hence the above command can also be performed using "v" as :

:v/pattern/d

You can match multiple pattern using "or" character "|" as bellow :

:v/pattern1\|pattern2\|pattern3/d

The above command deletes all the lines that does not contain the three patterns pattern1, pattern2 and pattern3.

No comments :

Post a Comment