How to Delete All Lines in Vim? [6 Cases]

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:Normal mode.Now type :%d and press the ENTER button:All lines are deleted in Vim.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:Normal mode.Now, press gg to move the cursor to the beginning of the file:Cursor at the beginning of the file.Now, press dG(d+SHIFT+g) to delete all lines of the file:All lines are deleted in Vim.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:Command for pattern delete.The lines containing “echo” will be deleted:all lines containing "echo" have been deleted in Vim.

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:Commands for range delete.The image below shows that lines from 3 to 5 have been deleted:From line 3 to line 5 have been deleted in Vim.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:Cursor initial position.This command will delete all the lines from the current cursor position up to the beginning of the file, as the following image shows:All lines before the cursor have been deleted in Vim.

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:Cursor initial position.This command will delete all the lines from the current line till the end of the file:All lines after the cursor have been deleted in Vim.

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:Normal mode before deletion.Now, I will type :10d and press the ENTER button:Line 10 has been deleted in Vim.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:Initial cursor position.It will delete the 3 three consecutive lines from the cursor point, as shown in the below image:Three lines have been deleted in Vim.

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:Blank line.The below image shows that the blank line has been deleted:Blank line has been deleted in Vim.

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


<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment