Vi or vim provides different commands for operations. One of these operations is deleting the line. Vi or vim provides different commands to delete single or more lines in different ways.
Delete Current Line
The command to delete the current line in Vi/Vim is
dd keys. The current line is the cursor’s current location. But the dd command should be called in the normal mode which accepts commands. Before issuing the dd command press the ESC key to be sure you are in normal mode.
- Press ESC
Pressing the dd keys multiple times delete multiple lines.
Delete Multiple Lines
The dd command can be also used to delete multiple lines. The number of lines should be specified before the dd command. The delete operation is applied after the cursor location. The following command will delete 3 lines after the cursor’s current location. But before issuing 3dd command press ESC to change to normal mode.
Delete Specified Lines or Range of Lines
The d command can be used to delete multiple lines for the specified range. The range will be specified with the line numbers where the start and end line numbers for deletion will be provided. First, we will change to the command mode with the ESC key. then type the following command in order to delete lines from 12 to 18.
Also following line specifiers can be used to set start and end lines according to the line numbers.
- . specifies the current line.
- $ specifies the last line.
- % specifies all lines.
Take a look at the following examples.
|:.,20d||Delete from the current line to line number 20.|
|:40,$d||Delete from line number 40 to the last line.|
|:.,$d||Delete from the current line to the end of the file.|
Delete From Current Line To The Begging of File
We can delete from the current line where the cursor is located to the beginning of the file with the following command.
- Press ESC
Delete From Current Line To The End of File
We can delete from the current line where the cursor is located to the end of the file with the following command.
- Press ESC
Delete All Lines
All lines can be deleted by using the
% specifier. In this case, you do not need to know the line numbers, the cursor number, etc. Just use the following command where a single line number specifier % will be provided.
Delete Lines Containing Specified String Pattern or Regex
Another useful case to delete lines in vi/vim is deleting lines those contain specified string pattern or regex. Every line containing the specified string pattern or regex is deleted by searched from the start of the file to the end of the file. The syntax is like below where the PATTERN is the string pattern or regex.
Delete All Lines Containing Given String
In the following example, we delete all lines that contain the string “linuxtect”.
Delete All Lines Those Do Not Contains Given String
In the following example, we reverse the deletion and delete lines that do not contain the string “linuxtect”. The
! sign is used for the reverse matches.
Delete All Lines Containing IP Address
In the following example, we remove all lines that do not contain an IP address.
Remove All Blank Lines
The following Vim/Vi command removes all blank lines.