How to Delete Lines in Vim

September 26, 2024

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.

Tutorial on how to delete Lines in Vim text editor

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:

Six lines in Vim file

To delete line number three, move the cursor to it and type dd:

Using Vim to remove a specific line

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:

Using Vim to delete a single word in a file

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:

A file with six lines open in Vim

To remove lines two to five, move the cursor to the second line and type:

3dd
Deleting multiple lines in a file using Vim

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
Deleting a range of lines in a file using Vim

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

Deleting a range of lines in a file using Vim - result

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
Delete all lines in a file using Vim

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

No lines in buffer output in Vim

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

Deleting all content from a specified line to the end of the file using Vim

The image below shows the end result.

Deleting all content from a specified line to the end of the file using Vim - 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
Deleting everything from the current line to the beginning of a file using Vim

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.

Vim showing lines containing a specific word

To delete all these lines, type:

:g /number/d
Using Vim to delete lines containing a specific word

The output shows the second, fourth, and sixth lines are deleted.

Using Vim to delete lines containing specific word - result

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
Using Vim to remove lines that do not contain a word

The output shows lines one, three, and five are deleted.

Using Vim to remove lines that do not contain a word - result

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.

Opening a text with empty lines in Vim

To delete these empty lines, type:

:g/^$/d
Using Vim to delete empty lines in a file

The output shows that empty lines are deleted.

Using Vim to delete empty lines in a file - result

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.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
22 Best Linux Text Editors for Programming & Coding
August 8, 2024

A text editor is an application that lets you type text. All Linux distributions come with built-in editors...
Read more
How to Cut, Copy and Paste in Vim / Vi
October 3, 2024

While working in Vim, copying, cutting and pasting text are frequently used shortcuts. In this tutorial...
Read more
Vim - Find and Replace
July 28, 2022

Finding and replacing text patterns in Vim simplifies navigation and changes in large files. Hence, Vim offers..
Read more
How to Undo and Redo Changes in Vim / Vi
October 10, 2024

Mastering basic Vim commands includes learning how to undo and redo changes in this text editor. Knowing how...
Read more