Delete blank lines using vi editor

Here's a very handy option to delete blank lines from a text file on Unix.

If it's a one time operation, you don't want to spend time writing a script for it.

Open the file using 'vi' editor [ # vi testfile.txt ]

Hit 'Esc' key to ensure you are in command mode.

Type :g/^$/d and hit Enter. You are done.

Here ^ means beginning of the line and $ means end of line (a blank line with no characters on it). 'd' means delete.

If there are space characters on the blank line, you could do this:

:g/^ *$/d

which means it will look for lines with one or more spaces and delete it.

Have fun.


User login

Who's new

  • amitbhagwat
  • TenLekerand
  • ClainaNaigh
  • RaviGunasekaran
  • gopasruhafoni

Who's online

There are currently 0 users and 1 guest online.