Vim Text Editing Deleting Blank Lines in Vim

Deleting Blank Lines in Vim

AS
Aman Saurav
| Jan 20, 2025 |
3 min read
#regex #editing

Vim allows you to manipulate text text using powerful global commands (:g). Deleting all blank lines in a file is a classic use case.

The Command

:g/^$/d

Breakdown

  • :g: Global command. Executes an action on all lines matching a pattern.
  • /^$/: The regex pattern. ^ is start of line, $ is end of line. ^$ matches a line that has nothing in it.
  • d: The action. Delete the matching lines.

Variations

  • Delete lines containing only whitespace (spaces/tabs):
    :g/^\s*$/d
    
  • Invert match (Delete non-blank lines):
    :v/^$/d