Open In App

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.

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,



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:

Syntax of command :[start],[end]d

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:

Examples:

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:

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:

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.

Article Tags :