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:
Edit Multiple Lines in Vi edtor
Next article icon

How to Delete Multiple Lines in Vi editor

Last Updated : 07 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the popular Linux distributions are Ubuntu, Cent OS, Red Hat, Debian, and Kali Linux.

In this article, we will cover how to delete multiple lines in the Vi editor we will look into the different methods to achieve it. First, we look at what Linux and VI editors are why we use them, what are its features and how can we use them, followed by a basic guide for the editor and different methods to delete multiple lines in the vi editor.

What is the Vi Editor?

Vi Editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. Vi editor was developed in 1976 by Bill Joy and later in 1991, an improved version of Vi editor was released which is known as VI IMproved (VIM).

There are two modes in Vi Editor:

  • Insert Mode: Used for editing and inserting text
  • Command Mode: Used for running commands like saving files, quitting the editor, and deleting lines.

Table of Content

  • Basic Guide to Using the Vi Editor
  • How to Delete a Single Line in the Vi Editor
  • How to Delete multiple lines in Vi editor
  • Delete lines that contain a specific pattern

Basic Guide to Using the Vi Editor

Here's a quick guide on how to use the vi editor:

1. Create/Edit a file using Vi editor

To create or edit a file, use the command:

vi filename 

2. Entering Insert Mode

To make any changes in the file first, you need to enter the insert mode to modify the file. To get into insert mode press the button 'i' to enter in insert mode.

3. Command Mode

To run any command in the vi editor you have to first enter the command mode if you are currently in the insert mode then press Esc and then ':' colon followed by your command to run your command in the editor.

4. Navigation in the editor

Use the following keys to navigate within the editor:

Key

Description

k

Moves the cursor up one line

j

Moves the cursor down one line

h

Moves the cursor to the left one-character position.

l

Moves the cursor to the right one-character position.

How to Delete a Single Line in the Vi Editor

To delete a single line follow the below steps:

  • Press Esc key if you are in insert/editing mode
  • Go to the file you want to delete
  • Press 'dd' and then the line got removed

How to Delete multiple lines in Vi editor

1. Delete a Specific Number of Lines

To delete multiple lines Press Esc to leave the insert/editing mode, enter the number of lines you want to delete followed by 'dd' i.e. ndd and the editor will delete the mentioned number of lines from the current line.

For Example:

3dd: Three lines including the current line got deleted.

2. Delete a range of lines

To delete a range of lines follow the below steps:

  • Press Esc to exit the insert/editing mode
 :[start],[end]d 
  • Replace [start] with the starting line number and [end] with the ending line number.
  • Press Enter to delete

Example: ":3,10d" in this command the editor will delete the lines from 3 to 10 including the extremes.

Example Screenshot:

Screenshot-from-2023-09-22-16-30-39

3. Using Wildcards to Delete Lines

You can also add wildcard characters in commands mentioned below:

  1. % (Percentage): Matches all the lines in the file
  2. . (Dot): Refers to the current line
  3. $ (Dollar): Denotes the end of the file

Examples:

  • :%d: Deletes all the lines from the file
  • :.,$d: Deletes the lines from current line to the end of file
  • :1,.d: Deletes the lines from the starting of file to the current line

Delete lines that contain a specific pattern

To delete lines based on a pattern using regular expression we use g command here g stands for global, syntax of commands is as follows:

:g/[pattern]/d - To delete the lines containing the pattern
:g!/[pattern]/d - To delete the lines that does not containes the pattern

Example:

  • :g/to/d: This command will delete the lines that contains 'to', note it also deletes the line which contain the a large word which contains to in it. See the below screenshot:

Screenshot-from-2023-09-22-16-53-13

  • :g!/to/d: This command will delete all the lines that does not contains the word 'to'

Screenshot-from-2023-09-22-16-58-08

To delete all the lines that starts with a particular character:

:g/^#/d - Replace # with the character you want to delete the lines that starts with.

Examples:

  • :g/^t/d - Delete all the lines that starts with 't'
  • :g/^s/d - Delete all the lines that starts with 's'

To delete all the lines that are empty:

:g/^$/d - Delete all the empty lines

Example:

Screenshot-from-2023-09-22-17-06-11

Conclusion

In this article, we had covered a basic Vi editor guide, followed by command to delete a single line and it's example followed by commands to delete multiple line based on constraints like deleting multiple line in a given range, deleting multiple lines based on pattern along with examples and we also discussed wildcard characters to optimise the commands.


Next Article
Edit Multiple Lines in Vi edtor
author
hardikkushwaha
Improve
Article Tags :
  • Linux-Unix
  • Geeks Premier League
  • Geeks Premier League 2023

Similar Reads

  • How to Delete Multiple Lines in vim Editor in Linux
    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 unwante
    7 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 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 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 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 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 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 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
  • how to Enter in Editing Mode in VI Editor
    The Vi editor, short for "Visual Editor," is a powerful and widely used text editor in the Unix and Linux operating systems. Vi offers various modes for text manipulation, including command mode, insert mode, and visual mode. In this article, we will focus on entering the editing mode, which is esse
    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