Edit Multiple Lines in Vi edtor
Last Updated :
24 Oct, 2023
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.
Linux
Linux is a kernel for operating system(OS) it is a core part of OS which was developed by Linus Torvalds in 1991. It is an open-source kernel that allows everyone to access the source code freely which makes it more popular and widely used in the industry as well as in personal systems. There are operating systems based on the Linux kernel, some of the popular Linux distributions are Ubuntu, Debian, Fedora, Kali Linux, etc.
VI Editor
Vi Editor is a text editor generally used in Unix/Linux systems and is well known for its efficiency and flexibility. Vi editor was developed by Bill Joy in 1976 and later an improved version of vi editor was released in 1991 which is known as VI IMproved (VIM) editor. There are two modes in the vi editor:
Edit multiple lines in vi editor
To edit multiple lines in once using vi editor follow the below steps,
- Press Ctrl+v and select the lines you want to edit you can use arrow keys or k,j to move upward and downward respectively.

- After selecting lines press Shift+i to enter in insert mode

- write the text you want enter
- when you are done with the editing part Press Esc to save and reflect the changes.

Delete multiple lines in vi editor
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.
Example: 3dd - Three lines including the current line got deleted.
Delete a range of lines
To delete a range of lines follow the below steps:
- Press Esc to exit the insert/editing mode
Syntax of command :[start],[end]d
- where start is the starting line and end in the ending line and both start and end line includes while deleting.
- Press Enter to delete
Example: :3,10d in this command the editor will delete the lines from 3 to 10 including the extremes.
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.
- :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:
Syntax - :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
Conclusion
In this article, we had covered how to edit multiple lines in vi editor 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 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 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 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 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
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
6 min read
vi Editor in Linux
The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor. Table of C
9 min read
Vim Editor in Linux
If youâre new to the world of Linux, one of the first things youâll encounter is the command line interface. And if youâre going to be working in the command line, youâll inevitably come across various text editors. Among them, Vim stands out as a powerful, yet notoriously complex, text editor. In t
7 min read
Editing Mode | Insert Mode in Vim Editor
This article will cover how to enter editing mode in Vim editor. First, we look at what Linux and VIM editors are and why we use them, followed by commands to enter editing mode in Vim editor along and examples and screenshots. LinuxLinux was developed by Linus Torvalds in 1991 as a hobby project. I
5 min read
Joining Two Lines in VI Editor
Vi is a powerful command-line text editor used by programmers, system administrators, and writers alike. They offer a vast array of features, including the ability to manipulate text efficiently. One common task you might encounter when working with Vi is joining two lines together. This article wil
3 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