Learn Practice Daily golf Reference

How to delete blank lines in Vim

Type :g/^$/d and press ↵ to delete every empty line. To catch lines that only hold spaces or tabs too, use :g/^\s*$/d instead.

Try it here

Tighten up the release notes

Some of these blank lines are empty and some hold a few spaces. :g/^$/d takes out the empty ones; then :g/^\s*$/d takes out the ones that only look empty.

1 of 2Delete the empty lines with :g/^$/d
1# Release notes
1
2- Faster search
3
4- New dark theme
5
6
7- Fixed the export bug
NORMALLoading the editor…1/20.0s1:1

This editor needs a keyboard. Read on, and play it at your computer.

TypeDeletes
:g/^$/d ↵every empty line
:g/^\s*$/d ↵every line that is empty or holds only spaces and tabs
:v/\S/d ↵the same, said the other way: every line without a non-blank character
:10,40g/^$/d ↵empty lines between lines 10 and 40 only
:'<,'>g/^$/d ↵empty lines in the lines you selected (press : in Visual mode and Vim types the range)
:%s/\s\+$//e ↵not a line delete: strips trailing spaces from every line, so whitespace-only lines become empty

How the command reads

:g (short for :global) runs a command on every line that matches a pattern. Here the pattern is ^$: ^ is the start of the line and $ the end, with nothing between them, so it only matches a line with no characters at all. The command after the last slash is d, delete.

In ^\s*$, \s is a space or a tab and * means any number of them, including none. So it matches empty lines and lines that only look empty, which is what you usually want in code that someone's editor left trailing spaces in.

When some blank lines should stay

Deleting every blank line also joins paragraphs that were meant to stay apart, as in the try above, where the heading now runs straight into the list. If you only want to clear a stretch of blank lines, select it with V and a motion and press d, or put the cursor on the first blank line and use a count: 3dd deletes three. Whatever you did, u undoes the whole :g in one go.