There are many ways to delete lines in Vim. This popular Linux text editor allows you to remove one, several, or specified patterns from a file with a single command.
In this tutorial, you will learn how to delete lines in Vim with multiple options per range or a pattern.
Options for deleting lines in Vim
Delete a single line
1. Press Esc to easily navigate the file.
2. Move the cursor to the line you want to delete.
2. Delete the
line by pressing one of the following commands:
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 delete.
3. Press the keys:
Delete multiple lines
1. Press Esc.
2. Move the cursor to the first line you want to delete.
3. Use one of the following commands (replacing [#] with the number of lines): [#]
In the following
example, we delete the following 3
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
For example, if you want to delete lines 4, 5, 6, and 7, you would use the command: :4,7d
Once you press Enter, the specified lines will be deleted, as in the image below
Delete all lines
1. First, press Esc.
2. Then, delete everything inside the file by typing: :
3. Once you run this command, the text editor will notify you that there are –No lines in the buffer-.
Delete from the current line at the end of file
1. Move the cursor to the first line you want to remove.
2. Make sure you are in command mode by pressing Esc.
3. Delete everything beyond that line with the command: :
The following example shows how to do this. According to the position of the cursor, the text will be deleted after executing the command.
The following image shows the final result.
Delete from the current line at the beginning of file
1. Move the cursor to the last line you want to delete.
2. Press Esc (if you are not already in command mode).
3. Delete everything from the current
line to the top of the text by typing: DGG
In this example, we delete the text in the red box by moving the cursor to the last line we want to delete
After typing the command, the file deleted these 6 lines
Delete lines based on
Vim pattern allows you to delete lines that match a specified pattern. There are several ways to use patterns to remove lines. Below you will find some of the most commonly used commands.
Remove lines that contain a specific word
1. First, use Esc to switch to normal mode.
2. Then, delete all lines that
contain a specific word by typing: :g /word/d
Delete lines that do not contain a specific word
1. Navigate to command mode with Esc.
2. Delete lines
that do not contain a specific word with the command: :
Delete all blank lines
1. To remove all blank lines, first switch to normal mode with the Esc key.
2. Then, type the following: :
After reading this article, you now have multiple options to delete lines and words in Vim. Knowing these commands can help you speed up your work with Vim as it doesn’t require manually deleting unwanted content.
Next, learn how to save and exit a file in Vim.