How to delete lines from Vim? How to delete ranges of lines? How to delete lines by a given pattern? Let’s cover in this article different ways to delete lines in Vim editor.
Install Vim in Ubuntu/Debian distributions
# sudo apt install vim
Install Vim in RHEL/CentOS distributions
# yum install vim
1) Delete a single line
To delete a single line in Vim editor, follow the steps below
- Place the cursor to the beginning of the line
- Press the ESC key.
- Next, press
ddi.e quick press letter ‘ d ‘ twice in quick succession.
In the example below, pressing dd at the beginning of line 6 as shown below will delete the entire line.
2) Delete all lines
Below are the two ways to delete all lines.
3) Delete multiple lines
To delete multiple lines
- place the cursor at the beginning of a line.
- Prefix the dd command with the number of lines you want to delete below it. For example, if you want to delete 3 consecutive lines below line 3 press
4) Delete a range of lines
If you want to delete a range of lines, say from line 3 to line 5, the syntax is as shown below
In this case, Press ESC Then type the command below and hit Enter.
To delete the last line
To delete all lines before the current line
To delete all lines after the current line
5) Delete lines by a given pattern
Finally, you can delete lines following a given pattern.
For instance, to delete lines that contain a certain word, press ESC and run
In our case, to delete lines that contain the word “lazy”
To delete every line that doesn’t contain the word “lazy”
To delete lines that begin with a certain letter, say ‘A’
If you want to delete lines that begin with a special character like $ sign, prefix the character with a backslash as shown
To get rid of all blank lines
Do you have any tips to delete vim lines? Hope this article helped you and please leave your comments.