Open In App

Edit Multiple Lines in Vi edtor

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

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:

  • Insert mode
  • Command mode

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.

Screenshot-from-2023-10-09-23-22-21

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

Screenshot-from-2023-10-09-23-24-00

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

Screenshot-from-2023-10-09-23-26-03

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads