> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Deleting Lines in Vim
- URL: https://itsfoss.com/vim-delete-lines/
- Published: 2023-10-25T03:43:04.000Z
- Updated: 2023-10-25T03:43:04.000Z
- Description: Single line, multi lines or all lines. Here's how to delete lines in Vim editor.
- Author: Sagar Sharma
- Tags: Vim, #hide

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](https://itsfoss.com/content/images/2023/10/Delete-single-line-in-Vim.gif)

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](https://itsfoss.com/content/images/2023/10/Delete-multiple-lines-in-Vim-editor.gif)

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](https://itsfoss.com/content/images/2023/10/Delete-specific-range-of-lines-in-Vim.gif)

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](https://itsfoss.com/content/images/2023/10/Delete-everything-from-file-in-Vim-editor.gif)

## 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](https://itsfoss.com/content/images/2023/10/Delete-lines-using-pattern-in-Vim.gif)

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](https://itsfoss.com/content/images/2023/10/Delete-empty-lines-in-Vim.gif)

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:](https://linuxhandbook.com/basic-vim-commands/?ref=itsfoss.com)

[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.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookAbhishek Prakash![](https://linuxhandbook.com/content/images/2020/06/basic-vim-command-guide-1.jpeg)](https://linuxhandbook.com/basic-vim-commands/?ref=itsfoss.com)

Once done, you can step up and learn [some tips and tricks to advance your Vim game:](https://linuxhandbook.com/pro-vim-tips/?ref=itsfoss.com)

[11 Pro Vim Tips to Get Better Editing ExperienceYou can learn plenty of Vim tips on your own, or you can learn it from others’ experiences.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookPratham Patel![](https://linuxhandbook.com/content/images/2022/08/pro-vim-tips.png)](https://linuxhandbook.com/pro-vim-tips/?ref=itsfoss.com)

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

![](https://itsfoss.com/content/images/2023/10/mastering-vim-quickly-book.jpg) 

#### Mastering Vim Quickly

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

[Chek it out](https://itsfoss.click/mastering-vim-quickly?ref=itsfoss.com) 

I hope you will find this helpful.