Delete empty lines from a file using vi editor

Use either of the following commands to delete all empty lines:

:g/^$/d
:v/./d

If you want to delete all lines that are empty or that contain only white space characters (spaces, tabs), use either of:

:g/^\s*$/d
:v/\S/d

In the second command, v operates on lines that do not match, and \S matches anything that is not a white space, and d deletes the flagged lines (all lines that have no characters, or that have only white space characters).

No comments: