Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to Edit Multiple Files in Vi Editor in Linux
Next article icon

How to Delete Multiple Lines in vim Editor in Linux

Last Updated : 09 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Table of Content

  • How to Delete Multiple Lines in Vim Editor?
  • How to Delete a Range of Lines in Vim Editor?
  • How to delete multiple lines in the Vim editor?
  • How to delete the line with a custom pattern in the Vim editor?
  • Additional Deletion Custom Pattern in Vim Editor
  • How to Delete Multiple Lines in vim Editor in Linux - FAQs

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 Vim
Opening File in Vim

Step 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) Mode
Switching from Insert to Edit (Command) Mode

Step 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 Command
Entering Delete Command

Step 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 Deleted
All Lines Deleted

How 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 Mode
Opening Vim in Edit Mode

Step 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 Delete
Entering Range of Lines to Delete

Step 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 Deleted
Range of Lines Deleted

How 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 Mode
Vim Editor Opened in Visual Mode

Step 2: Then we need to select the lines using the up and down keys of the keyboard.

Lines Selected to be deleted
Lines Selected to be deleted

Step 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 d key to delete multiple lines
Pressed the d key to delete multiple lines

How 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 Mode
Switched from Insert to Edit Mode

Step 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 Word
Entered Custom Pattern to Delete Lines containing Ubuntu Word

Step 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 Ubuntu Word
Lines Deleted consisting of Ubuntu Word

Additional 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 lines
Delete all lines except Ubuntu word containing lines

Step 2: After executing this pattern the lines except the lines containing Ubuntu words will get deleted.

Lines deleted except lines consisting Ubuntu word
Lines deleted except lines consisting of Ubuntu word

Pattern 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 letter 'T'
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 'T' letter
Lines deleted starting with the 'T' letter

Pattern 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 File
Blank Lines in File

Step 2: Enter the command to delete the black lines from the document.

Entering Command to Delete Blank Lines
Entering Command to Delete Blank Lines

Step 3: After hitting the "Enter" key all the blank lines will be removed.

Blank Lines Deleted
Blank Lines Deleted

Conclusion

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.


Next Article
How to Edit Multiple Files in Vi Editor in Linux

G

gpancomputer
Improve
Article Tags :
  • Linux-Unix
  • Geeks Premier League
  • Geeks Premier League 2023

Similar Reads

  • How to comment multiple lines in Vim Editor in Linux
    Vim Editor in Linux is one of the most used text editors for writing scripts, codes, and textual data. There are different types of features offered by the Vim Editor through which we can create the text more comfortably. Various types of shortcuts, modes, and bindings are available in Vim Editor. W
    5 min read
  • How to Delete Multiple Lines in Vi editor
    Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kernel that is most popular and widely used in the industry as well as in personal systems. There are various operating systems based on the Linux kernel, some of th
    5 min read
  • How to Edit Multiple Files in Vim Editor in Linux
    Vim, short for "Vi Improved," is a highly configurable and powerful text editor built upon the foundation of Vi. Like its predecessor, Vim offers robust support for editing multiple files simultaneously, making it an indispensable tool for developers, system administrators, and anyone working extens
    4 min read
  • How to Edit Multiple Files in Vi Editor in Linux
    Vi, short for "visual editor," is a powerful and ubiquitous text editor in the Linux ecosystem. Among its many features, one of the most useful is its capability to edit multiple files simultaneously. This ability streamlines the editing process, especially when dealing with projects that span multi
    4 min read
  • How to Display Line Number in vim editor in Linux
    In this article, we will cover how to make the Vim editor display or hide line numbers. First, we look at what Linux and VIM editors and why we use them, are its features and how can we use them Follow the basic VIM editor guide to get familiar with the editor followed by various options to make the
    5 min read
  • Edit Multiple Lines in Vi edtor
    In this article, we will cover how to edit multiple lines in vi editor. First, we look at what Linux and VI editors followed by editing multiple lines in the vi editor with examples and then we will look how to delete multiple lines in the vi editor. LinuxLinux is a kernel for operating system(OS) i
    4 min read
  • How to Delete Line in Nano Editor?
    In a terminal environment, Nano is a straightforward and widely used text editor for Linux and macOS systems. Deleting lines in Nano is a basic yet essential operation, allowing for efficient editing within files. By following simple keyboard shortcuts like "Ctrl+K," you can swiftly remove lines fro
    5 min read
  • How to undo in Vim editor in Linux
    Vim (short for Vi Improvised) is a popular text editor mainly used in Linux and Unix-based operating systems but due to having a CLI (command line interface), it might be difficult for new users to make use of basic features such as undo in Vim. So in this article, we will learn different ways and t
    3 min read
  • How to Indent Code in Vim Editor?
    Vim is a popular text editor used by many developers for writing code, creating readme files, and writing scripts. It's open-source and offers a lot of ways to customize your experience. This makes it easier and more efficient to work with. Properly formatting and indenting your code is important be
    4 min read
  • How to Hide Line Numbers in Vim Editor
    In this article, we will cover how to hide line numbers in the Vim editor. First, we look at what Linux and VIM editors and why we use them, what are its features and how can we use them followed by various options to make the Vim editor hide line numbers and how we can change the default settings o
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences