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
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:
Delete a Single Word
1. Start by moving into command mode with
2. Place the cursor at the beginning of the word you want to remove.
3. Press the keys:
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
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):
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
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:
For instance, if you wanted to remove lines 4, 5, 6, and 7, you would use the command:
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
2. Then, delete everything within the file by typing:
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
3. Delete everything beyond that line with the command:
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:
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:
Remove Lines that Don’t Contain a Specific Word
1. Navigate to command mode with
2. Remove the lines that do not contain a specific word with the command:
Delete All Blank Lines
1. To delete all blank lines first move to normal mode using the
2. Then, type in the following:
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.