Introduction

There are many ways to delete lines in Vim. This popular Linux text editor allows you to remove one, multiple, or specified patterns from a file using a single command.

In this tutorial, learn how to delete lines in Vim with multiple options by range or a pattern.

Tutorial on how to delete Lines in Vim text editor

Options for Deleting Lines in Vim

Delete a Single Line

1. Press Esc to navigate through the file easily.

2. Move the cursor to the line you want to delete.

2. Delete the line by pressing one of the following commands:

dd
D

Delete a Single Word

1. Start by moving into command mode with Esc.

2. Place the cursor at the beginning of the word you want to remove.

3. Press the keys:

dw

Note: If you want to customize the look of your Vim console, read our article on working with Vim color schemes.


Delete Multiple Lines

1. Press Esc.

2. Move the cursor to the first line you want to remove.

3. Use one of the following commands (replacing [#] with the number of lines):

[#]dd
d[#]d

In the example below, we deleted the following 3 lines:

How to delete multiple lines in Vim.

After running the command, the file consists of the remaining lines:

Removing multiple lines in Vim text editor

Delete a Range of Lines

1. Use Esc to switch to normal mode in Vim.

2. Use the following command and replace [from] with the number of the line where you want the range to start and [to] where you want it to end:

:[from],[to]d

For instance, if you wanted to remove lines 4, 5, 6, and 7, you would use the command:

:4,7d

Example showing How to delete ranges of lines

Once you press Enter, the specified lines are removed, as in the image below.

screenshot of Removed lines

Delete All Lines

1. First, press Esc.

2. Then, delete everything within the file by typing:

:%d

3. Once you run this command, the text editor will notify you there are --No lines in buffer--.

vim delete all lines command

Delete From Current Line to the End of File

1. Move the cursor to the first line you want to remove.

2. Ensure you are in command mode by pressing Esc.

3. Delete everything beyond that line with the command:

:.,$d

The following example shows you how it is done. According to the position of the cursor, the text will be removed after running the command.

removal of lines

The image below shows you the end result.

Deleting lines from cursor to the end of the file

Delete From Current Line to the Beginning of File

1. Move the cursor to the last line you want to delete.

2. Press Esc (if you are not in command mode yet).

3. Delete everything from the current line to the top of the text by typing:

dgg

In this example, we deleted the text in the red box by moving the cursor to the last line we want removed.

Delete lines by a given pattern

After typing the command, the file removed these 6 lines.

6 lines removed

Delete Lines According to Pattern

Vim allows you to delete lines that match a specified pattern. There are various ways you can use patterns to remove lines. Below you will find some of the most commonly used commands.

Remove Lines Containing a Specific Word

1. Firstly use Esc to move to normal mode.

2. Then, remove all the lines that contain a specific word by typing:

:g /word/d

Remove Lines that Don't Contain a Specific Word

1. Navigate to command mode with Esc.

2. Remove the lines that do not contain a specific word with the command:

:g!/word/d

Delete All Blank Lines

1. To delete all blank lines first move to normal mode using the Esc key.

2. Then, type in the following:

:g/^$/d

Conclusion

After reading this article, you now have multiple options for deleting lines and words in Vim. Knowing these commands can help you speed up working with Vim, as you don’t require manually removing unwanted content.

Next, learn how to save and exit a file in Vim.


Next you should also read