Vim is an easily configurable text editing software used to create or modify text files in Linux. It is an improved version of Vi editor, inheriting the shortcuts and features of Vi with adding several advanced features which make it the choice for Linux users. As discussed throughout this article, built-in shortcuts are used to modify the text, such as to delete lines (in a particular way) in Vim and press the respective shortcuts.
This article will explain how to delete lines in Vim (editor).
How to Delete Lines in Vim?
This article will discuss the methods to delete lines in Vim using different examples using the following shortcuts to delete lines in Vim.
Shortcuts to Delete the Lines in Vim
The below shortcut keys delete the lines in the Vim editor:
|dd||To delete an entire line.|
|Shift + D||This deletes the text from the cursor to the line’s end.|
|3dd||Deletes the three lines, including the cursor position line and the next two lines.|
|dgg||Delete from the current line to the first line.|
|:[to],[from]d||Delete from a specific line to the desired line.|
|:%d||Deletes all the lines in the entire document.|
|:.,$d||The current and the following lines (till the end of the document) are deleted.|
|:g/^$/d||Deletes all the empty lines.|
|:g /word/d||Remove a line that contains a specific word.|
|:g!/word/d||Remove all the lines that don’t contain a specific word.|
Percentage (%): This sign shows the complete (all lines) document.
Dollar ($): Shows the end of the line.
Let’s discuss the deleting of lines in Vim with the help of examples.
Delete a Single Line in Vim
In this section, we will delete a single line with different patterns with the help of examples.
Example 1: Delete a Single Line (dd)
We can use the shortcut key dd to delete a single line in Vim. To delete a single line, follow the below steps:
To open a file testfile.txt in Vim, execute the following command:
$ vim testfile.txt
The below interface will appear:
Press the “Esc” key for the normal mode and move the cursor to that specified line:
Press the “d” button from the keyboard twice (dd) to delete the entire line in Vim:
Note: These three steps will be performed for all the below examples. The shortcut keys and command will be entered in the Normal mode (Using the Esc key).
Example 2: Delete from Specific Word to End of Line (Shift + D)
To delete from a specific word to the end of the line, the “Shift + D” shortcut key is used in the normal mode (Press Esc key):
The below output shows that the highlighted text is deleted:
Delete Multiple Lines in Vim
There are several ways to delete multiple lines or patterns in Vim; let’s delete the lines with the help of different examples.
Example 1: Delete Multiple Lines
Several lines can be deleted simultaneously by pressing the desired number and the “dd”. For instance, to delete two (2) lines, navigate to the specific line, press the 2 from the keyboard, and then press d twice (dd) as shown below:
Both the lines are deleted, as can be seen in the below output:
Example 2: Delete Specific Lines in Vim
Vim allows you to delete specific lines. In this case, we will delete lines 4 to 6 by using:
Three lines are deleted from 4 to 6, and the status also verifies that 3 fewer lines from the below output:
Example 3: Deletes All the Empty Lines
If you have several empty lines in your document and want to delete them, Add the “:g/^$/d” in VIm and Press Enter:
The empty lines are removed from the document as can be seen in the below output:
Example 4: Delete from Selected Line to Start of the Document
If you want to delete text from a specific line to the start of the document, navigate to that line (as shown below) and press “dgg” shortcut keys:
The output shows that lines from the cursor position to the start are deleted which can be verified by looking at the “3 fewer lines” in the below picture:
Example 5: Delete Lines Containing a Specific Word
We can delete all the lines which contain a specific word. For instance, to delete all the lines in the documents having the word “test” in them, use the “:g /test/d” syntax and press the Enter key:
The three lines containing the “test” word are deleted as shown below:
Example 6: Delete Lines That Do Not Contain a Specific Word
If you want to delete all lines that don’t have a specific keyword you want to find, use the “:g!/word/d” by replacing the word with the desired search word (in this case, test):
Three lines are remaining which contain the “test” word, and all other lines (9) are removed from the document, as seen in the below output:
Delete All Lines of the Entire Document in Vim
All the lines from a document can be deleted at once, let’s see how to do it.
Example 1: Delete All Lines of the Entire Document (:%d)
Sometimes, we want to delete all the lines from the document, use the “:%d” or “:1,$d” command in normal mode to do so (as can be seen in the below picture):
:%d OR :1,$d
All lines from the document is deleted, as shown in the below picture:
This guide has provided all details for deleting the lines from vim.
Vim text editor is used for modifying (adding, editing, or deleting lines) the file text, which can be done using shortcuts. We need to delete lines in different ways, which can be done using the built-in shortcuts mentioned in this article. For instance, to delete a single line (dd), delete two lines (2dd), and for deleting a line with a specific word (:g /test/d).