How to Delete Line in Vim on Linux

April 22, 2020

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: Can’t find the word you want to delete? Take a look at how to search for a word in vim.

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

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

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

Note: Check out our guide on how to show lines in Vim.

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.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
22 Best Linux Text Editors for Programming & Coding
September 3, 2019

A text editor is an application that lets you type text. All Linux distributions come with built-in editors...
Read more
How to Change and Use Vim Color Schemes
March 9, 2020

Vim color schemes are a handy feature for setting syntax highlighting. You can choose from a wide variety of...
Read more
How to Show or Hide Line Numbers in Vim / Vi
February 28, 2022

You can activate line numbering in Vim by showing absolute, relative or hybrid lines. Displaying or hiding...
Read more
How to Undo and Redo Changes in Vim / Vi
April 28, 2020

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