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.
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:
After running the command, the file consists of the remaining lines:
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
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
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--
.
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.
The image below shows you the end result.
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.
After typing the command, the file removed these 6 lines.
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
SysAdmin,DevOps and Development
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,…
SysAdmin,DevOps and Development
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…
SysAdmin,DevOps and Development
How to Show or Hide Line Numbers in Vim / Vi
April 15, 2020
You can activate line numbering in Vim by showing absolute, relative or hybrid lines. Displaying or hiding…
SysAdmin,DevOps and Development
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…
Author
Sofija Simic
Sofija Simic is an aspiring Technical Writer at phoenixNAP. 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.