Install any Linux distribution, and you will see that Vim and Vi are available and often set as the default editors.
Expertise in using these text editors is considered a benchmark of your familiarity with Linux environments. In this detailed tutorial, we will discuss how to delete a line in Vim. Despite the dated appearance, you will be surprised to learn that we have covered 10 ways of removing a single or multiple lines in Vim.
Let’s dive into the details of these examples and see how you can enhance your Vim editing game!
10 Easy Ways of Deleting Lines in Vim
Vim offers several ways to delete a single line, multiple lines, or lines based on a specific pattern.
Example #1: Delete a Single Line in Vim
To delete a single line in Vim, you can either use the dd command or the single D (Shift+d) command. Here are the steps of the process:
Step #1: Press esc
Step #2: Move the cursor to the line you want to delete.
Step #3: Press dd OR D
Note that D is short for the delete command. The line will be removed immediately. Once the changes are done, you should save the file with :wq in command mode.
Example #2: Delete a Single Word in Vim
To delete a single word, place the cursor at the beginning of the word and press dw. Here is a description of the process:
Step #1: Press esc
Step #2: Move the cursor to the beginning or the first character of the word
Step #3: Press dw
This command deletes the word your cursor is on, starting from the current cursor position.
When dealing with a large file, efficiently locating and deleting specific words becomes essential. Explore our tutorial for easy tips on searching for words in Vim.
Example #3: Delete Multiple Lines
If you need to delete multiple lines simultaneously, use the d command followed by a number and then d again. Follow these steps for this process:
Step #1: Press the escape(esc) key
Step #2: Move the cursor to the first line you want to delete.
Step #3: Press #dd or d#d (replace # with the number of lines you want to delete).
For instance, use 5dd or d5d to delete five lines, starting from the line where the cursor is currently located.
Example #4: Delete a Range of Lines
You can also delete a range of lines by specifying the starting and ending lines. For instance, here are the steps of the process of deleting lines 2 to 4:
Step #1: Press ESC
Step #2: Type :2,4d and press Enter.
Example #5: Delete All Lines
To delete all lines within a file, type :%d and then press Enter.
Important: We advise caution because this command wipes the entire content of the file.
Example #6: Delete From the Current Line to the End of the File
The process of deleting everything from the current line to the end of the file has the following steps:
Step #1: Press Esc
Step #2: Move the cursor to the first line you want to delete
Step #3: Type :.,$d and press Enter
Example #7: Delete From the Current Line to the Beginning of the File
Reversing the previous example, you can delete from the current line to the beginning of the file. The process has the following steps:
Step #1: Press Esc
Step #2: Move the cursor to the last line you want to delete
Step #3: Type dgg and press Enter
Navigating through a Vom document is a matter of using several quick commands. We have an entire tutorial on how to quickly jump to the start or end of a File in Vim.
Delete Lines According to a Pattern
You can use a pattern to instruct Vim to delete lines matching the specified pattern. Vim offers several methods to achieve this objective. We will discuss the following methods you can use to remove lines based on patterns.
Example #8: Remove Lines Containing a Specific Word
To delete all lines containing a specific word, use the :g command.
For instance, to delete all lines containing Bill, type :g/BIll/d and press Enter.
Example #9: Remove Lines that Don’t Contain a Specific Word
Conversely, to delete all lines that DO NOT contain a specific word, use the :v command. For example, :v/is/d will delete all lines that do not have the word success.
Example #10: Delete All Blank Lines
A common use case for deleting lines in Vim is to remove blank lines from the document.
To remove all blank lines from your file, use the command :g/^$/d. This command looks for lines that have no characters (blank lines) and deletes them.
Conclusion
Mastering the deletion commands in Vim can significantly enhance your text editing efficiency on Linux. Whether you’re dealing with a single word, a specific line, multiple lines, or lines that match a certain pattern, Vim provides a command to get the job done.
FAQs
Q. Can I undo a deletion in Vim?
Yes, simply press u to undo the last action, including deletions.
Q. How do I save my changes after deleting lines in Vim?
After deleting lines, press :w to save your changes or :wq to save and quit Vim.
Q. Is it possible to delete lines without opening Vim?
Yes, you can use Vim in Ex mode from the command line. For example, vim -c ‘g/error/d’ -c ‘wq’ file.txt will delete all lines containing “error” in file.txt and then save and close the file.
Q. How can I learn more Vim commands?
Vim has a built-in tutorial. Just type vimtutor in your terminal to start an interactive learning session.
Q. How to delete a single line in vi?
To delete a single line in vi, you can position the cursor on the line you want to delete and then in normal mode, press ‘dd’.
Q. Can I delete multiple lines in vi at once?
Yes, you can delete multiple lines in vi by specifying the number of lines you want to delete followed by ‘dd’. For example, to delete five lines, you can use ‘5dd’.
Q. How to delete multiple lines containing a pattern in vim?
You can delete multiple lines containing a pattern in vim using the command ‘:g/pattern/d’ where ‘pattern’ is the text you want to match in the lines to delete.
Q. What is the difference between normal mode and insert mode in vi?
Normal mode in vi is used for navigation, editing, and executing commands, while insert mode is used for entering text into the file. You can switch between these modes by pressing ‘Esc’ for normal mode and ‘i’ for insert mode.
Q. How to delete a specified number of lines in vi at once?
To delete a specified number of lines in vi at once, you can use the command ‘ndd’ where ‘n’ is the number of lines you want to delete.
Q. How do I delete lines using Visual mode in Vim?
To delete lines using Visual mode in Vim, first, enter Visual mode by pressing v or V depending on whether you want to select characters or whole lines. Then, navigate to the lines you wish to delete, and once selected, press d to delete them.
Q. How can I delete a line from the current cursor position in Vim?
You can delete a line from the current cursor position in Vim by using the command dd. This command will delete the line where the cursor is currently positioned.