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:
- Press the
Esc
key to switch to normal mode. - Use arrow keys to go to the line that you want to remove.
- Press
dd
and that line will be removed.
Still confused? Let me show you how to pull that off:
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:
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:
- Press the
Esc
key to switch to normal mode. - Press the colon key
:
and executeset nu
to show the number of lines. - 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
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:
- Press the
Esc
key to switch to normal mode. - 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 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 matchPATTERN
: Enter the pattern you want to matchd
: 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
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
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:
Once done, you can step up and learn some tips and tricks to advance your Vim game:
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.
I hope you will find this helpful.