vim – delete all blank lines

To delete all blank lines in vim:

:g/^$/d
  •  “:” -> enter command-line mode
  • “g” -> do the action globally (apply it to all lines to which the subsequent match applies)
  • “/^$/” -> regex to mean “a line is a match if it contains nothing from start to finish”
  • “d” -> the action to do for matches is “delete”

Note: to see the lines which would be deleted without actually deleting them, just remove the action (“d”):

:g/^$/

Of course, you can undo any mistakes with: ESC then u

Sharing is caring!

Leave a Comment

Your email address will not be published. Required fields are marked *