To delete all lines in Vim, first switch to normal mode. Then type :%d
and press the ENTER button. It will delete all lines of the file in Vim. In this article, I have explored some other approaches to deleting lines in Vim, which will guide users to efficiently manage files and edit them when necessary. So, without further delay, let’s start!
1. Deleting All Lines in Vim
Deleting all lines in Vim enables users to start their script anew. To delete all lines in Vim, utilize the commands :%d
or gg dg
. In the following section, I have discussed these approaches elaborately.
A. Executing “%d”
If a file contains many unnecessary lines, it is better to delete them all and start again from scratch. In this example, I have demonstrated a way to delete all lines of a file while editing using Vim. To do this, first, switch to normal mode:Now type :%d
and press the ENTER button:The image shows that all the lines are deleted.
B. Using “gg dg”
To delete all lines in Vim using gg dg
, first, switch to the normal mode:Now, press gg
to move the cursor to the beginning of the file:Now, press dG
(d+SHIFT+g) to delete all lines of the file:The image shows that all lines of the file have been deleted.
2. Deleting Lines Containing a Pattern
To delete lines containing a pattern in the file, execute the command :g/<pattern>/d
. Here I have a file where I will delete the lines containing “echo”. To do this, first, I will switch to normal mode. Then type :g/echo/d
and press ENTER:The lines containing “echo” will be deleted:
3. Deleting a Range of Lines in Vim
To delete a range of lines in Vim, follow the syntax :[start],[end]d
. This will delete a range of lines inside a file. Here, I will delete from line 3 to line 5. To do this, first switch to normal mode. Then type :3,5 d
and press the ENTER:The image below shows that lines from 3 to 5 have been deleted:Below, I have listed two scenarios for deleting lines from the cursor position:
A. Deleting from Current Line to the Beginning of File
To delete lines from the current line to the beginning of the file, first switch to the normal mode. Then, move the cursor to the line from where you want to delete. After that, type dgg
:This command will delete all the lines from the current cursor position up to the beginning of the file, as the following image shows:
B. Deleting from Current Line to the End of File
To delete from the current line to the end of the file, first switch to the normal mode. Then, move the cursor to the line form where you want to delete. Then, type : $d
and press the ENTER button:This command will delete all the lines from the current line till the end of the file:
4. Deleting a Specific Line in Vim
To delete a specific line in Vim, first switch to the normal mode. Then type :<line number>d
and press ENTER. It will delete the specified line. Here, I will delete the line number 10. So first, I will switch to the normal mode:Now, I will type :10d
and press the ENTER button:The image shows that the line 10 has been deleted.
5. Deleting Multiple Lines in Vim
To delete multiple lines in Vim, first switch to the normal mode. Now, place the cursor on the line from where you want to delete lines. Then type <number of line>dd
and press ENTER. Here, I will delete three lines. So I will type 3dd
and finally press the ENTER button:It will delete the 3 three consecutive lines from the cursor point, as shown in the below image:
6. Deleting All Blank Lines in Vim
To delete all blank lines in Vim, first switch to the normal mode. Then type :g/^$/d
and press the ENTER button. In my case, there is a blank line on line 6 (see the below image). This command will delete this blank line:The below image shows that the blank line has been deleted:
Conclusion
This article discussed various approaches to deleting lines in the Vim editor. It is a beginner’s Vim guide to the deletion process of lines inside a file. I appreciate your valuable insight into this article. So don’t forget to put them in the comment box if you have any.
People Also Ask
How do I delete all lines in Vim editor?
To delete all lines in the Vim editor, first switch to normal mode. Then press gg
to move the cursor to the beginning of the file. Now press dG
(d+SHIFT+g) to delete all lines until the end of the file.
How do I delete multiple lines in Vim visual?
To delete multiple lines in Vim, first press ESC to switch to the normal mode. Now, place the cursor to the line from where you want to delete lines. Then type <number of line>dd
and press ENTER.
How do I delete 100 lines in vi editor?
To delete multiple lines in Vim, first press ESC to switch to the normal mode. Now, place the cursor on the line from where you want to delete lines. Then type 100dd
and press ENTER. It will delete 100 lines from the current cursor position.
How to select all in Vim?
To select all in Vim, first switch to normal mode. Then press gg VG
. This will select all the text of the file in Vim.
Related Articles
- How to Show Line Numbers in Vim? [All Scenarios]
- How to Go to a Specific Line in Vim? [All Cases]
- How to Select All Text in Vim? [5 Cases]
<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics
FUNDAMENTALS A Complete Guide for Beginners