The Wayback Machine - https://web.archive.org/web/20240926172946/https://www.geeksforgeeks.org/how-to-delete-multiple-lines-in-vi-editor/
Open In App

How to Delete Multiple Lines in Vi editor

Last Updated : 02 Oct, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Linux

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.

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
  • Command Mode

Basic Vi Editor Guide

1. Create/Edit a file using Vi editor

vi filename

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

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.

Delete a Single Line

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

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.

Example Screenshot:

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

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:

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

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.



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 unwanted lines, then deleting them one by one will be a t
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. While developing a code, we usually need to comment
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 will guide you through the process of joining lines i
3 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 from your text. Additionally, Nano includes an undo f
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 multiple files. In this article, we'll delve into the t
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 extensively with text files in Linux environments. In th
4 min read
How to Comment and Uncomment multiple line vi terminal editor
The Vi and Vim text editors are powerful tools widely used by programmers, system administrators, and writers for efficient text manipulation and editing. A common task that often arises during coding or text management is the need to comment or uncomment multiple lines of code or text. Commenting helps in annotating code for better understanding,
3 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) it is a core part of OS which was developed by Linu
4 min read
CodeLobster IDE - free cross-platform PHP, HTML, CSS, JavaScript editor
CodeLobster IDE - free cross-platform PHP, HTML, CSS, JavaScript editor In this article, we suggest you get acquainted with the free editor of web languages - CodeLobster IDE. It is presented on the software market for a long time already, and it wins a lot of fans. CodeLobster IDE allows you to edit PHP, HTML, CSS and JavaScript files, it highligh
3 min read
Encrypting Files Using vim editor in Linux
Vim, the popular text editor comes with a built-in feature to encrypt files with a password. Vim uses algorithms like Blowfish to encrypt files. It is faster and convenient than some other utilities available to do so. To get started, all you need is a system with a full version of vim installed. Some Linux distributions are shipped with a minimal
3 min read
Difference between vi Editor and cat Command
Both vi editor and cat command are used to input contents in a file. 1. vi Editor : vi editor stands for the visual editor which is an intelligent editing tool in UNIX Operating System used for reading, creating files, editing them, and more. It is the default editing tool in UNIX. Command to open a particular file : vi filename It has three modes
3 min read
How to Install Atom Text Editor in Linux?
"Atom - A Hackable Text Editor of 21st Century", as the line indicates Atom is an open-source, cross-platform, feature enrich text editor developed by Github. Atom has attractive features like cross-platform editing, built-in package manager, smart auto-completion, file-system browser, and many more. Apart from features, its beautiful sleek design
3 min read
AMP Text Editor for Linux Terminal
Amp is a fully-featured and lightweight text editor inspired by the Vi/Vim text editor. Amp editor is a terminal-based editor, and it is written in the Rust language. It provides the all fundamental features required for a modern text editor. Amp does not need any configuration it comes with all configuration, and it does not use any plugins. And i
3 min read
Micro - Lightweight terminal based text editor
Micro is a terminal-based text editor, and it is very easy to install and use. The micro editor is developed to be the successor of nono editor. It has no external decencies and no external files are needed. It is a single program. Now let's see the features of the micro text editor. FeaturesEasy to install and use and easy to configureNo external
3 min read
How to install Tiled Map Editor on Ubuntu
The Tiled Map is a free and open-source level editor that runs on a variety of platforms. It allowed us to alter tile maps in orthogonal, isometric, and hexagonal projections. We can make numerous layers and tiles of any size using it. Tiled Map Editor 1.8.5 is the most recent version at the time of writing this article. Snap may be used to install
2 min read
Nano vs VIM editor – What’s the difference between nano and vim editors?
In this article, we will understand the difference between nano and vim editors in Unix/Linux. Before diving into this topic, we will first understand what exactly nano and vim editor is. Nano Editor in the Linux System. Nano is a simple, user-friendly text editor that improves user experience. Its GUI (Graphical User Interface) makes it easy to us
3 min read
How to install Master PDF Editor on Ubuntu
A strong tool for creating or editing existing PDF documents is the Master PDF Editor. Keep reading to learn how to install the Master PDF editor on Ubuntu Linux as it is not accessible through the default repository. When you install Master PDF Editor, you can edit PDF files and save your changes in addition to opening and viewing them. The free e
3 min read
Installing Kate text editor on Linux
In Linux, as there are various text editors like Vi and Vim, but all these editors are terminal-based, there is no responsive User Interface in these editors, so we can install UI-based text editors on Linux. There is one of the editors named Kate, which is a popular choice of many Linux users, as this editor is Open-software, it is free to use. Th
5 min read
How to Save a File in Vi / Vim Editor & Quit
In this article, we will cover how to save a file in vi editor. First, we look at Linux and Vi editors why we use them, and what their are features then we deep dive into the topic of saving a file and quitting the editor along with the other options, we understand the different commands with an example. What is Linux?Linux was developed by Linus T
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 types to undo changes in Vim Editor in Linux. Like
3 min read
Best Text Editor for Kali Linux
Kali Linux is one of the most popular Linux distros, With a special focus on penetration testing and ethical hacking. It is especially popular among those who want to try their hand at ethical hacking. But just as with any other Linux distro, to make the best use of your operating system, you need the best applications. In this article, we will loo
8 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 vim editor display or hide line numbers and how w
5 min read
Quit VI Editor Without Saving Changes in Linux
In this article, we will cover how to quit the vi editor without saving your changes in Linux. First, we look at what Linux and VI editor and why we use them, what are its features and how can we use them. LinuxLinux 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) kern
4 min read
Make the VI Editor Display or Hide Line Numbers
In this article, we will cover how to make the vi editor display or hide line numbers. First, we look at what Linux and VI editor and why we use them, what are its features and how can we use them followed by various options to make the vi editor display or hide line numbers and how we can change the default settings of vi editor to display or hide
3 min read
Searching and Replacing in Vim Editor
Vim is an excellent version of the legacy Vi editor for terminals. It is a complete command line-based text editor that has been the go-to choice for IT professionals for decades. Vim is the Vi-IMproved version of the original text editor which contains many modern features. In this article, we shall explore one such feature, the functionality to s
7 min read
Is vi editor free?
Yes ! vi is a free and open-source text editor. Vi editor was developed in 1976 by Bill Joy, vi editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. An improved version of Vi editor was released later in 1991, popularly known as VI IMproved (VIM). Why are vi editor occurrences free?VI editor is
5 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 of Vim editor to hide line numbers. LinuxLinux was
3 min read
Vi vs. Vim: Choosing the First Right Text Editor
Vi and Vim are both text editors used primarily in Unix-like operating systems, although they are available on various platforms. They both are powerful text editors known for their efficient text manipulation and keyboard-driven interface. Whether you should learn Vi before Vim depends entirely on you, your requirements, and your specific goals an
3 min read
Enabling Syntax Highlighting in Vim Editor
In this article, we will cover how to highlight syntax in Vim editor. First, we look at what Linux and VIM editors followed by syntax highlighting in the Vim editor with examples and screenshots we will also look at different color schemes available and how we can change the default settings of Vim editor to enable syntax highlighting by default. V
3 min read
How to install Gparted partition editor on Linux
some tools areOn Linux systems, managing disk partitions can be challenging, especially for new users. Fortunately, some tools are easy to use which can make this procedure simpler. GParted, is a user-friendly partition editor that enables users to manage their disk partitions. In this article, we will discuss the GParted tool in the Linux Operatin
3 min read