How to Delete Blank Lines in Vim

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

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.

Posted in these interests:

vim
PRIMARY
23 guides

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

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

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.

Posted in these interests:

vim
PRIMARY
23 guides

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

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

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.

Posted in these interests:

vim
PRIMARY
23 guides

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

Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020

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.

Posted in these interests:

vim
PRIMARY
23 guides

How to Delete Blank Lines in Vim

vim
Tyler Tyler (285)
Total time: 2 minutes 
Updated: July 22nd, 2020
Tyler
 

Posted in these interests:

vim
PRIMARY
23 guides
vim
PRIMARY
23 guides
PRIMARY
Calling all writers!

We’re hiring. Write for Howchoo

 
In these interests
vim
PRIMARY
23 guides
vim
PRIMARY
23 guides
PRIMARY

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.

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!
Calling all writers!

We’re hiring. Write for Howchoo

Tyler's profile pictureTyler
Joined in 2015
Software Engineer and creator of howchoo.
Tyler's profile picture
Share this guide!
RedditEmailText
Related to this guide:
Vim undo branchesVim undo branches
You probably already know how to use vim’s basic undo and redo features. For a quick refresher, read this short tutorial on how to use vim redo and undo.
Dayne's profile picture DayneView
In these interests: vim
3 Ways to Use Tabs in Vim3 Ways to Use Tabs in Vim
Vim tabs can be a great way to visualize which files you currently have open. Many vim users get bogged down by them and either end up working too hard to use them or give up altogether.
Dayne's profile picture DayneView
In these interests: vim
Vim redo and undoVim redo and undo
Learn how to undo and redo changes using vim. If you are brand new to vim, learn more about the basics in this intro to vim guide.
Dayne's profile picture DayneView
In these interests: vim
Vim undo branchesVim undo branches
You probably already know how to use vim’s basic undo and redo features. For a quick refresher, read this short tutorial on how to use vim redo and undo.
Dayne's profile picture DayneView
In these interests: vim
Dayne's profile pictureViewvim
3 Ways to Use Tabs in Vim3 Ways to Use Tabs in Vim
Vim tabs can be a great way to visualize which files you currently have open. Many vim users get bogged down by them and either end up working too hard to use them or give up altogether.
Dayne's profile picture DayneView
In these interests: vim
Dayne's profile pictureViewvim
Vim redo and undoVim redo and undo
Learn how to undo and redo changes using vim. If you are brand new to vim, learn more about the basics in this intro to vim guide.
Dayne's profile picture DayneView
In these interests: vim
Dayne's profile pictureViewvim
People also read:
Once you learn to think in text objects, you’ll never be the same
One of the strengths of Vim is that it’s highly customizable. And while remapping keys is possible, we often want to create shortcuts without changing the default key bindings.
If you find yourself highlighting text in Vim with your mouse to copy and paste it elsewhere, stop. There’s a better way, using the yank command, to copy text into your clipboard on macOS or Windows.
Basic vim commands
Because you’ve accidentally opened Vim and don’t know how to exit
Vim is amazing. Well, Python is doing all the work, but I still like Vim. Learn how to format JSON in Vim like a pro with this short guide! tl;dr Paste your json, and run this in vim normal mode. :%!
Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.
Need to change the case of characters to all caps or all lowercase? This is easily done using Vim. This guide will show you how to change the case of characters in Vim.
Trailing whitespace can be a real pain – especially when diffing changes between two versions of a file.
Vundle, Pathogen, Vim-plug, VAM, and the others can be great time savers but sometimes you just have to do something yourself so you can understand what exactly the time saving software is doing for you in the background. For myself, I got sick of not understanding what these were doing to my system. I realized I did not know the fundamentals of vim plugins so I set out to learn. This is a step by step guide to teach you how to install a basic vim plugin without the help of any fancy manager.
Once you learn to think in text objects, you’ll never be the same
One of the strengths of Vim is that it’s highly customizable. And while remapping keys is possible, we often want to create shortcuts without changing the default key bindings.
If you find yourself highlighting text in Vim with your mouse to copy and paste it elsewhere, stop. There’s a better way, using the yank command, to copy text into your clipboard on macOS or Windows.
Basic vim commands
Because you’ve accidentally opened Vim and don’t know how to exit
Vim is amazing. Well, Python is doing all the work, but I still like Vim. Learn how to format JSON in Vim like a pro with this short guide! tl;dr Paste your json, and run this in vim normal mode. :%!
Vim Text Objects: Using Vim Text Objects
How to Change Your Leader Key in Vim
Vim: How to Copy to Your System Clipboard
Basic vim commands
Basic vim commandsBasic Vim Commands
How to Format JSON in Vim
Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.
Need to change the case of characters to all caps or all lowercase? This is easily done using Vim. This guide will show you how to change the case of characters in Vim.
Trailing whitespace can be a real pain – especially when diffing changes between two versions of a file.
Vundle, Pathogen, Vim-plug, VAM, and the others can be great time savers but sometimes you just have to do something yourself so you can understand what exactly the time saving software is doing for you in the background. For myself, I got sick of not understanding what these were doing to my system. I realized I did not know the fundamentals of vim plugins so I set out to learn. This is a step by step guide to teach you how to install a basic vim plugin without the help of any fancy manager.
Vim: Convert Tabs to Spaces
How to Change or Switch the Case of Characters in Vim
Ack Search, Open Files in Vim
Vim: How to Remove Trailing Whitespace on Save in Vim
How to install Vim plugins without a plugin manager
Posted in these interests:
vimvim
vim
PRIMARY
vimvim
vim
PRIMARY
PRIMARY
Explore
Discuss this guide:
We’re hiring!
Are you a passionate writer? We want to hear from you!
We’re hiring!
Are you a passionate writer? We want to hear from you!
View openings

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.

Donate

Leave a Reply

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