How to Delete Blank Lines in Vim
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines. Fortunately there is a simple Vim command to remove all blank lines in a file.
tl;dr
:g/^$/d
See below for more details.
While in normal mode, type:
:g/^$/d
Here’s a breakdown of this command.
| Description | |
|---|---|
| :g | This will execute a command globally on all lines that match a regex (to follow). |
| /^$/ | This is the regex. The forward slashes enclose the pattern, which is: ^$. This is a regex pattern that matches the beginning of a line (^) and the end of a line ($) with nothing in between, which is the definition of a blank line 🙂 |
| d | This is the command to be executed. d stands for delete! |
So when you put it all together, you find all blank lines globally across the whole file, and delete them.
How to Delete Blank Lines in Vim
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines. Fortunately there is a simple Vim command to remove all blank lines in a file.
tl;dr
:g/^$/d
See below for more details.
While in normal mode, type:
:g/^$/d
Here’s a breakdown of this command.
| Description | |
|---|---|
| :g | This will execute a command globally on all lines that match a regex (to follow). |
| /^$/ | This is the regex. The forward slashes enclose the pattern, which is: ^$. This is a regex pattern that matches the beginning of a line (^) and the end of a line ($) with nothing in between, which is the definition of a blank line 🙂 |
| d | This is the command to be executed. d stands for delete! |
So when you put it all together, you find all blank lines globally across the whole file, and delete them.
How to Delete Blank Lines in Vim
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines. Fortunately there is a simple Vim command to remove all blank lines in a file.
tl;dr
:g/^$/d
See below for more details.
While in normal mode, type:
:g/^$/d
Here’s a breakdown of this command.
| Description | |
|---|---|
| :g | This will execute a command globally on all lines that match a regex (to follow). |
| /^$/ | This is the regex. The forward slashes enclose the pattern, which is: ^$. This is a regex pattern that matches the beginning of a line (^) and the end of a line ($) with nothing in between, which is the definition of a blank line 🙂 |
| d | This is the command to be executed. d stands for delete! |
So when you put it all together, you find all blank lines globally across the whole file, and delete them.
How to Delete Blank Lines in Vim
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines. Fortunately there is a simple Vim command to remove all blank lines in a file.
tl;dr
:g/^$/d
See below for more details.
How to Delete Blank Lines in Vim
vimWhile in normal mode, type:
:g/^$/d
Here’s a breakdown of this command.
| Description | |
|---|---|
| :g | This will execute a command globally on all lines that match a regex (to follow). |
| /^$/ | This is the regex. The forward slashes enclose the pattern, which is: ^$. This is a regex pattern that matches the beginning of a line (^) and the end of a line ($) with nothing in between, which is the definition of a blank line 🙂 |
| d | This is the command to be executed. d stands for delete! |
So when you put it all together, you find all blank lines globally across the whole file, and delete them.
While in normal mode, type:
:g/^$/d
Here’s a breakdown of this command.
| Description | |
|---|---|
| :g | This will execute a command globally on all lines that match a regex (to follow). |
| /^$/ | This is the regex. The forward slashes enclose the pattern, which is: ^$. This is a regex pattern that matches the beginning of a line (^) and the end of a line ($) with nothing in between, which is the definition of a blank line 🙂 |
| d | This is the command to be executed. d stands for delete! |
So when you put it all together, you find all blank lines globally across the whole file, and delete them.
While in normal mode, type:
:g/^$/d
Here’s a breakdown of this command.
| Description | |
|---|---|
| :g | This will execute a command globally on all lines that match a regex (to follow). |
| /^$/ | This is the regex. The forward slashes enclose the pattern, which is: ^$. This is a regex pattern that matches the beginning of a line (^) and the end of a line ($) with nothing in between, which is the definition of a blank line 🙂 |
| d | This is the command to be executed. d stands for delete! |
So when you put it all together, you find all blank lines globally across the whole file, and delete them.
:g/^$/d
| Description | |
|---|---|
| :g | This will execute a command globally on all lines that match a regex (to follow). |
| /^$/ | This is the regex. The forward slashes enclose the pattern, which is: ^$. This is a regex pattern that matches the beginning of a line (^) and the end of a line ($) with nothing in between, which is the definition of a blank line 🙂 |
| d | This is the command to be executed. d stands for delete! |
Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.
