Delete Multiple Lines in Vim

We all make mistakes. Making mistakes is human. Improving upon it is also important.

If you make a mistake in Vim, you can delete a word by using dw in normal mode. You type dd and it deletes the current line.

If you want to delete multiple lines in Vim, you can use the same dd Vim command by adding the number of lines to it.

So, 10dd will delete 10 lines from the bottom of the cursor (including the line that the cursor is at).

Let's take a detailed look at how you can delete one or more lines in an editor that is famous for its efficiency.

Delete single line

Below are the steps on deleting a single line of text in Vim:

  1. Enter the Normal mode by pressing the Escape (Esc) key
  2. Make sure that your cursor is on the line that you want to delete.
  3. Quickly press dd

That will delete the entire line that your cursor is on.

As you can see in the gif, I am on the first line and running the dd command deleted only the first line.

Specify number of lines to delete

Before you use the dd command to delete a line, you can specify a number before it to delete several lines at once in Vim.

Below is the syntax:

:[num]dd

When you specify number in place of num, Vim will start deleting lines. You can think of this as Vim is performing the dd command for num times.

As evident from the gif, I ran the command 12dd (it appears in the bottom-right corner) and it deleted the block of 12 lines.

Specify a range of lines to delete

Instead of specifying how many lines to delete from the current line to bottom, you can just tell Vim from which line it should start deleting and up to which line.

It will work better if you have enabled line number display in Vim.

Below is the syntax to delete a range of lines:

:[begin],[end]d

Let's break this syntax down...

  • : (colon) indicates that it is a Vim command.
  • begin tells Vim what line to begin from
  • end tells Vim what the last line to delete is
  • d the delete command in Vim

Moreover, below are the characters used to specify the range (used for 'begin' and 'end):

  • . (current line)
  • $ (last line in file)
  • % (all lines)

To learn from a few examples, let's take a look at them.

  • :10,45d - Delete from 10th line to 45th line
  • :.,$d - Delete from current line till the last line
  • :.,1d - Delete from current line to beginning of the file
  • :dgg - Delete from current line to beginning of the file

As you can see in this gif, even though I am on line 31, and I typed :10,29d and it deleted lines from 10 to 29.

Delete all lines

As mentioned earlier, the % symbol is used to specify the range from beginning to the end of file, essentially all lines.

This means, to delete all lines of a file in Vim, you need to use :%d command.

Delete lines that match a certain pattern

Apart from all these Vim wizardry, did you know that you could delete lines that matched a particular pattern?

Well, you can, and here is the syntax.

:g/PATTERN/d

To do the opposite of this, deleting the lines that do not match the specified pattern, you can use the syntax specified down below.

:g!/PATTERN/d

Breaking this down, we get the following elements:

  • g - search globally (i.e. the entire file)
  • ! - reverse match
  • PATTERN - the pattern to match
  • d - delete command

If you run the following command in Vim, it will remove all the lines that have "extern crate" pattern in the line.

:g/extern\ crate/d

Delete all empty lines

To delete empty lines, it is necessary to use regex for pattern matching. Below is the command for perform deletion of empty lines:

:g/^$/d

In regex, the ^$ pattern means any line that starts with a newline, essentially an empty line.

Conclusion

This article goes in depth on how you can delete lines in Vim. Here's a quick summary of what you learned in this article. Feel free to save it and share it on social media:

If you are interested in learning more than just the Vim Basics, I highly recommend using this program by Jovica Ilic.

Mastering Vim Quickly - Jovica Ilic
Exiting Mastering Vim Quickly From WTF to OMG in no time