How to Delete Multiple Lines in Vi editor
Last Updated :
07 Oct, 2024
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.
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:

3. Using Wildcards to Delete Lines
You can also add wildcard characters in commands mentioned below:
- % (Percentage): Matches all the lines in the file
- . (Dot): Refers to the current line
- $ (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:

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

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:

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.
Explore
Linux/Unix Tutorial
5 min read
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting