AIX Tip of the Week

Search and Delete Lines within vi

Audience: All

Date: March 22, 2003

Here are two techniques for globally deleting lines from within the vi editor.

The first is an "ed" command, which deletes all lines containing the work "junk".

[ESC]:%g/junk/d

The second is a filter command that uses multiple search criteria. This example deletes all lines containing apples, oranges or grapes. [ESC]:% ! egrep -v "(apples|oranges|grapes)"

In both cases, the [ESC] is the "Escape" key. The "%" is shorthand for "all lines." Alternatively, you could limit the changes to specific lines. For example if you were to change "%" to "50,100", only lines 50 through 100 would be searched.


Bruce Spencer,
baspence@us.ibm.com