To delete single or multiple lines in Linux, you need to be familiar with the vi or vim editor. You will usually delete a line or several lines using the editors mentioned above.
So, Let’s take a look at the different ways that you can use to delete line in vim & vi editor under Linux.
How to delete a single line in vi & vim editor?
It is most comfortable to delete line in vim using the dd command. Of course, it is simple. To achieve it, You need to press the letter “d” twice in rapid succession, don’t worry, it’s easy. It’s like double-clicking.
Here are the needed steps to delete a single line using vi:
- Move to the line you wish to delete.
- Use The ESC key to enter the command mode. You need it to ensure that you are in command mode.
- To delete the line, press dd.
Multiple hits of dd will delete various lines. However, each line is deleted once at the same time. Because it is so simple to delete one line, I frequently use it to delete multiple lines at once.
How to delete multiple lines in vi & vim editor?
It is easy to delete multiple lines if you know the number of lines you want to remove. It’s easy as deleting a single line where The dd command will be prefixed with the number you wish to delete.
Here are the required steps to delete multiple lines:
- Move to the first line you wish to delete
- Use the ESC key
- Next, press the number of lines that you want to delete. for example: 3dd 4dd 5dd 6dd 9dd
Please note that you should remove the lines in a sequence or consecutive.
How to delete lines within a range in vi or vim editor?

If you know the numbers of the range’s start and end lines, you can delete lines within a range. It is similar to the case we discussed in the previous section. It is the general syntax to delete a group of lines:
:[start number],[end number]d
To delete all lines between line numbers 111 and 234, follow these steps
- Hit the ESC key
- type :111,234d
- Enter
How to delete all lines in vi or vim editor?
To delete all lines in vim, you can use the same syntax. You can use other methods to empty and truncate a file, but this will only work if the editor is open.
- Hit the ESC key
- type :1,$d
- Enter
The $ symbol denotes the end of the file. The above command will remove lines beginning with the first line and ending on the last line.
How to delete all lines preceding the current line using vi or vim editor?
You can also delete lines in vim that are before the cursor or the current line in your editor. The current line in the editor is denoted by the character.
- Hit the ESC key
- Type :1,.-1d
- Enter
The command above will delete all lines beginning with the first line and ending at the last line.
How to delete all lines following the current line using vi or vim editor?
You can delete lines from the line after the current one by using the dot (.). To denote the current line, use the dot (.) and the dollar ($ to denote its last line.
- Hit the ESC key
- Type :.+1.$d
- Enter
- How to delete lines that are consistent with a pattern using vi or vim editor?
If you need to delete certain characters or words, you can also search and delete them. For the case where you need to delete all the lines in your file that include the word ‘saloon_,’ then
- Hit the ESC key
- type :g/saloon/d
- Enter
- How to delete lines that don’t match the pattern using vi or vim editor?
You will need to remove lines that don’t match a particular pattern before deleting them. To delete lines without the word “saloon,” you will need to
- Hit the ESC key
- type :v/saloon/d
- Enter
- How to delete all empty lines using vi or vim editor?
You can use a variation to delete all the blank lines from a file.
- Hit the ESC key
- type: g /^$/d
- Enter
The vi editor has a powerful pattern-matching tool. You can learn the vi pattern matching commands and use them for any text manipulation you come across.
To ensure that you are operating in command mode, I recommend hitting the ESC key every time you enter a command. This step is unnecessary if you know you are already in command mode.