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 using different options.
Deleting Lines in Vim
Deleting lines enhances text editing efficiency when working in Vim. Vim provides several commands to quickly remove single or multiple lines.
The following text explains how to delete lines in Vim using different methods.
Delete a Single Line
Deleting a single line in Vim is straightforward. To accomplish this, open a file and press Esc to navigate through the file easily.
Move the cursor to the line you want to delete and type one of the following:
dd
D
For example, access Vim and create a file with six lines:
To delete line number three, move the cursor to it and type dd
:
The output shows that line number three has been removed.
Note: Learn how to show or hide line numbers in Vim for easier navigation.
Delete a Single Word
To delete a single word, enter the normal mode using Esc and place the cursor at the beginning of the word you want to remove. Next, press the keys:
dw
For example, to delete the word This from the first line, move the cursor to that word and press dw
:
Note: Can't find the word you want to delete? Take a look at our guide on how to search for a word in Vim.
Delete Multiple Lines
To delete multiple lines, enter the normal mode and move the cursor to the first line you want to remove.
Use one of the following commands (replacing [#]
with the number of lines):
[#]dd
d[#]d
For example, the file has six lines:
To remove lines two to five, move the cursor to the second line and type:
3dd
Delete a Range of Lines
To delete a range of lines, use Esc
to switch to normal mode in Vim and run the following command:
:[from],[to]d
Replace [from]
with the number of the line where you want the range to start and [to]
where you want it to end.
For instance, if you want to remove lines two, three, four, and five, use:
:2,5d
Once you press Enter, the specified lines are removed, as in the image below:
Note: If you want to customize the look of your Vim console, read our article on working with Vim color schemes.
Delete All Lines
To delete all lines, enter the normal mode first. Next, type the following:
:%d
Once you run this command, the text editor notifies you there are --No lines in buffer--
.
Delete From Current Line to the End of File
To delete everything from the current line to the end of the file, enter the normal mode and move the cursor to the first line you want to remove.
Delete everything beyond that line using the command:
:.,$d
For example, delete everything from the second line by moving the cursor there and typing:
:.,$d
The image below shows the end result.
Delete From Current Line to the Beginning of File
To delete everything from the current line to the beginning of the file, enter the normal mode first. Move the cursor to the last line you want to delete and type in the following:
dgg
For example, delete everything from the fifth line to the top by moving the cursor to that line and typing:
dgg
Delete Lines Containing a Specific Word
To delete lines with a specific word, use Esc
to move to normal mode. Next, remove all the lines that contain the specified word:
:g /word/d
For example, some of the following lines contain the word number.
To delete all these lines, type:
:g /number/d
The output shows the second, fourth, and sixth lines are deleted.
Delete Lines that Don't Contain a Specific Word
You can also remove the lines that don't contain a specific word. To do that, switch to normal mode and run the following:
:g!/word/d
For example, to remove all lines that don't contain the word number, type:
:g!/number/d
The output shows lines one, three, and five are deleted.
Delete All Blank Lines
To delete all blank lines, move to normal mode using the Esc
key. Next, type in the following:
:g/^$/d
For example, the following text has every other line blank.
To delete these empty lines, type:
:g/^$/d
The output shows that empty lines are deleted.
Note: Check out our guide on how to show lines in Vim.
Conclusion
This article explained multiple options for deleting lines and words in Vim. Knowing these commands helps you speed up working with Vim, as you don't have to manually remove unwanted content.
Next, learn how to save and exit a file in Vim or check out our free, downloadable Vim commands cheat sheet.