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.
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