Tags

, , , , , ,

1. Show all the lines that match a pattern, say PATTERN
:g/PATTERN

Use “g!” or “v” for negative matching. For example, to show all the lines that DO NOT match the pattern, PATTERN, use
:g!/PATTERN

2. Delete all the lines that contain the pattern, PATTERN
:g/PATTERN/d

Again, g! or v may be used for negative matching.

Application: You may use any of the following two commands to delete all the blank lines (including lines containing only whitespaces) from a file
:g/^\s*$/d
:v/\S/d

Reference: Vim Wiki.

About these ads