Deleting Lines in Vim

Single line, multi lines or all lines. Here's how to delete lines in Vim editor.
Warp Terminal

Want to delete lines in Vim? Well, it is pretty easy as all you have to do is press dd and it will remove the line where your cursor is located.

Sure you can use dd multiple times to remove more lines but that's not Vim user would do (like never).

So here are some different ways to delete lines for different scenarios (when you are in normal mode with Esc key):

Action Description
dd Delete the current line.
[num]dd Delete multiple lines at once.
:[begin],[end]d Delete a specific range of lines.
:%d Delete everything.
:g/PATTERN/d Delete lines matching a certain pattern.
:g/^$/d Delete empty lines.

Looks complex? Let me share more details.

Delete a single line in Vim

To delete a single line, you have to follow 3 simple steps:

  1. Press the Esc key to switch to normal mode.
  2. Use arrow keys to go to the line that you want to remove.
  3. Press dd and that line will be removed.

Still confused? Let me show you how to pull that off:

Delete single line in Vim

That was quick. Right?

Delete multiple lines at once in Vim

This is similar to pressing the dd but solves one problem. Let's suppose that you want to remove 4-5 lines and in that case, you have to press the dd command multiple times.

But in Vim, you can specify how many times you want to run a specific keybinding. Such as if you want to use dd 4 times, then you use 4dd and it will remove 4 lines from the cursor:

delete multiple lines in Vim

If you notice, in the above example, I used 4dd twice!

Delete a specific range of lines in Vim

There are times when you want to remove an entire block of code and in that case, specifying the range of line is the most practical option.

For this, you need to follow 4 simple steps:

  1. Press the Esc key to switch to normal mode.
  2. Press the colon key : and execute set nu to show the number of lines.
  3. Now, press the colon key again and specify the range of lines in this format: :[begin],[end]d.

Here, inside of [begin], enter the number of a line from where you want to start the removal process and at the [end] specify the end line to be removed.

For example, if I want to remove line number 4 to 10, then my command would look like this:

:4,10d
delete specific range of lines in Vim

Pretty easy. Right?

Delete everything from a file in Vim

To delete everything in Vim, all you have to do is follow these 3 simple steps:

  1. Press the Esc key to switch to normal mode.
  2. Press the colon key : and execute %d and it will remove everything.

Still confused? Here's how you delete everything from a file in Vim:

Delete everything from file in Vim editor

Delete lines by a matching pattern in Vim

From here enter the magic of Vim. I mean deleting lines that match specific patterns is cool. Isn't it?

For that, first, press the Esc key to enable normal mode and then execute the matching pattern in the following manner:

:g/PATTERN/d

Alternatively, if you want an opposite effect to keep lines only matching the pattern and remove everything else, use the following:

:g!/PATTERN/d

Now, let's breakdown the above command:

  • g: Perform global search (search from the entire file)
  • !: Reverse match
  • PATTERN: Enter the pattern you want to match
  • d: Deletes line

For example, if I want to delete a line having the term filename, then I will be using the following:

:g/filename/d
Delete lines using pattern in Vim

As you can see, it removed every line that had the term filename in it.

Delete empty lines in Vim

Using the pattern, you can delete empty lines in Vim.

For that, first, make sure you are in normal mode using the Esc key and once, press the colon key : and execute the following:

:g/^$/d
delete empty lines in Vim

As you can see when I executed the :g/^$/d pattern, it removed all the empty lines in my file.

That's it from my side.

Let me help you become better at Vim

New to Vim and want to get started as soon as possible? Start with the basic commands of Vim including a cheat sheet:

Basic Vim Commands Every Linux User Must Know [With PDF Cheat Sheet]
A comprehensive guide explaining basic vim commands that will be useful to any Linux user be it a sysadmin or a developer.

Once done, you can step up and learn some tips and tricks to advance your Vim game:

11 Pro Vim Tips to Get Better Editing Experience
You can learn plenty of Vim tips on your own, or you can learn it from others’ experiences.

Or, take your Vim skill to the next level with this highly rated book.

Mastering Vim Quickly

Master Vim like a true professional with this highly rated, premium Vim book and training course.

Chek it out

I hope you will find this helpful.

About the author
Sagar Sharma

Sagar Sharma

A software engineer who loves to write about his experience with Linux. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.