How to Delete Multiple Lines in vim Editor in Linux
Last Updated :
09 Feb, 2024
Vim Editor is a Linux terminal-based editor that has various advanced features for editing, but due to its terminal behavior, it is quite difficult to handle some of the operations in Vim. While making a text file, we often need to delete some unwanted lines. If the document consists of more unwanted lines, then deleting them one by one will be a time-consuming task, but in Vim we can delete multiple lines easily by different approaches. In this article, we will all the ways to delete multiple lines, and also we will see some more steps to delete a range of lines, delete an entire lines of the file, and also we will see the regular expression-based deletion of lines.
How to Delete Multiple Lines in Vim Editor?
In this section, we will see how we can delete entire lines from Vim Editor in a single go rather than deleting them one by one. So to delete all lines in Vim Editor, follow the below-specified steps:
Step 1: In the first step, we need to open the document in the Vim editor for usage using the below command. After executing the command the file will get opened in the editor on the terminal itself.
vim file.txt
Opening File in VimStep 2: After opening the file, we need to press the 'ESC' key on our keyboard to switch from the insert mode to the edit mode.
Switching from Insert to Edit (Command) ModeStep 3: After switching to the Edit Mode, we need to enter the command through which we can remove or delete all lines from the Vim Editor. Type the below command and hit Enter on the keyboard.
:%d
Entering Delete CommandStep 4: After hitting enter, we can see that entire lines from the document or file will get deleted in a single go rather than deleted one by one.
All Lines DeletedHow to Delete a Range of Lines in Vim Editor?
In this section, we will see how we can remove or delete the specific range of lines in Vim Editor. Follow the below-described steps to delete the range of lines.
Step 1: After opening the file in the Vim Editor, we need to use the 'ESC' key to switch from the Insert mode to the Edit Mode.
Opening Vim in Edit ModeStep 2: After switching to the edit mode, we need to enter the command through which we can delete the specific range of lines.
:3,8d
Entering Range of Lines to DeleteStep 3: After entering this command and hitting the Enter Key, we can see that the specified range of lines from the document has been successfully deleted.
Range of Lines DeletedHow to delete multiple lines in the Vim editor?
In this section, we will see how we can remove the multiple consecutive lines from the Vim Editor. Follow the below steps to delete multiple lines in Vim Editor.
Step 1: After opening the document in the Vim editor, we need to enter the Visual Mode by pressing the key Shift + V on the Keyboard.
Vim Editor Opened in Visual ModeStep 2: Then we need to select the lines using the up and down keys of the keyboard.
Lines Selected to be deletedStep 3: After selecting the lines, we need to press the 'd' key on the keyboard to delete the selected line permanently from Vim Editor.
Pressed the d key to delete multiple linesHow to delete the line with a custom pattern in the Vim editor?
In this section, we will see the regular expression used to delete the line from Vim Editor. As Vim is an advanced editor, we can use the custom pattern to delete the lines as per our customization.
Step 1: After opening the file in the Vim Editor, we need to press the 'ESC' key on the keyboard to change the mode from Insert to Edit mode.
Switched from Insert to Edit ModeStep 2: Now we need to enter the pattern through which the deletion will be done. In the below command, we have given the pattern through which the lines will be deleted that contains the word 'Ubuntu'.
:g/Ubuntu/d
Entered Custom Pattern to Delete Lines containing Ubuntu WordStep 3: After entering the command and hitting the Enter key, the lines containing the word Ubuntu will get deleted from the document.
Lines Deleted consisting of Ubuntu WordAdditional Deletion Custom Pattern in Vim Editor
In this section, we will see more additional deletion custom patterns in Vim Editor to delete the lines in Vim Editor.
Pattern 1: Delete All Lines except Line Containg Specific Word
Step 1: If we want to delete all lines except the line containing the specific word then we can use the below pattern. For example, we will delete all lines except the lines containing the "Ubuntu" word.
:g!/Ubuntu/d
Delete all lines except Ubuntu word containing linesStep 2: After executing this pattern the lines except the lines containing Ubuntu words will get deleted.
Lines deleted except lines consisting of Ubuntu wordPattern 2: Deleting Lines that Begin with Specific Letter
Step 1: If we need to delete the lines that begin with the specific letter, we can use the below pattern in Vim Editor.
:g /^T/d
Deleting lines starting with the letter 'T'Step 2: This pattern will delete all the lines that start with the letter 'T' in the entire document.
Lines deleted starting with the 'T' letterPattern 3: Deleting all Blank Lines
Step 1: Using this pattern we can delete all the blank lines that are present in our document. We just need to use the below-mentioned pattern.
:g /^$/d
Blank Lines in FileStep 2: Enter the command to delete the black lines from the document.
Entering Command to Delete Blank LinesStep 3: After hitting the "Enter" key all the blank lines will be removed.
Blank Lines DeletedConclusion
In conclusion, the deletion operation in Vim Editor consists of various combinations of commands and navigations. We can use different modes like Visual Mode, and Command Mode to delete the lines from the file. Along with this, the custom patterns also also useful for deleting the lines that satisfy the given input pattern, We can delete the lines according to the range, and specific word consisting lines, and also using the pattern, we can delete the blank lines from the file. This allows ease for the user to manage the lines and code inside the file.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Example of an AVL Tree:The balance factors for different nodes are : 12 :1, 8:1, 18:1, 5:1, 11:0, 17:0 and 4:0. Since all differences
4 min read