You often need to delete one or more lines while editing text files in Vim editor, and unlike any other text editor, it allows you to edit your files quite efficiently. This guide will focus on how to edit and delete lines from your files in the Vim editor.
If you don’t have Vim editor, then try installing it using:
Vim lets you delete lines in various ways, for instance, you can delete all lines at once, multiple lines, and even lines by custom pattern/word. Let’s check all of these methods:
Deleting a single line in Vim editor:
The process of deleting only one line in Vim is simple. To delete a line, follow the steps below:
- First, bring your cursor to the line you want to delete.
- Press the “Esc” key to change the mode.
- Now type, “:d”, and press “Enter” to delete the line or quickly press “dd”.
I am removing line number 3 for demonstration. See the images below:
How to delete all lines in Vim editor:
To delete all the lines at once, follow the process below:
- First, hit the “Esc” button to switch mode from insert to edit.
- Now type, “:%d”, and hit “Enter” to remove all the lines.
How to delete a range of lines in Vim editor:
Vim also allows to select a range of lines and then delete them. The syntax is given below:
For instance, if you want to delete lines from line number 5 to line number 7, then simply follow the below-mentioned process:
- Change the mode using the “Esc” key if it is in insert mode.
- Type “:5,7d” and hit “Enter”, line number 5,6, and 7 will be removed.
If you want to delete all the lines before the current line, then use “:1,-1d“. For example, if you want to delete lines from line number 5 to all lines before this lines, then use the following method:
- Bring your cursor to line number 5.
- Press the “Esc” key and type “:1,-1d”, and then press “Enter”.
To delete all line after line number 5, follow the below-mentioned process:
- Bring the cursor to line number 5.
- Press the “Esc” key and type “:+1,$d”, then press “Enter”, the lines that are below line number 5 will be removed.
How to delete multiple lines in Vim editor:
In this method, we will learn the process of removing multiple consecutive lines. Simply follow the process:
- Bring the cursor to a line from where you want to start deleting.
- If you want to remove 4 consecutive lines, then simply type “4dd”.
In the above image, the cursor is on line 2, therefore lines 3,4,5, and 6 have been removed.
How to delete the line with a custom pattern in Vim editor:
Another feature of Vim editor is that it allows you to delete the line with a given condition, the syntax is given below:
Replace “<WORD>” with the word of the line you want to delete. For example, if you want to delete lines with the word “Linux”, then use the following approach:
- Press the “Esc” key to change mode.
- Type “:g /linux/d”, then hit “Enter”.
The lines containing the “Linux” word will be deleted.
Similarly, if you want to delete all lines, except lines containing the “Linux” word, then use
To delete lines that begin with a specific letter, use
To remove all blank lines in your text file or code, use
Conclusion:
Vim is one of the well-liked text editors among Linux users and developers. It is a free, well documented, and customizable text editor. In this guide, we learned how to edit text and code in Vim. We thoroughly discussed various methods and approaches to delete a line, multiple lines, and even lines with a specified word. Vim is a versatile editor that has a lot more features to uncover.