Open In App

How to Delete Multiple Lines in Vi editor

Last Updated : 02 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads