vim

How to delete lines in Vi/Vim?

Vim previously known as Vi, stands for “Vi IMproved”, is a multi-purpose text editor that is compatible with all UNIX based operating systems like Linux and macOS. Vim can be used for various purposes such as creating, editing text, and/or computer program files.

You often need to delete one or more lines while editing text files in Vim editor, and unlike any other text editor, it allows you to edit your files quite efficiently. This guide will focus on how to edit and delete lines from your files in the Vim editor.

If you don’t have Vim editor, then try installing it using:

$ sudo apt install vim

line/1%20copy.png

Vim lets you delete lines in various ways, for instance, you can delete all lines at once, multiple lines, and even lines by custom pattern/word. Let’s check all of these methods:

Deleting a single line in Vim editor:

The process of deleting only one line in Vim is simple. To delete a line, follow the steps below:

  1. First, bring your cursor to the line you want to delete.
  2. Press the “Esc” key to change the mode.
  3. Now type, “:d”, and press “Enter” to delete the line or quickly press “dd”.

I am removing line number 3 for demonstration. See the images below:

line/multi%201.png

How to delete all lines in Vim editor:

To delete all the lines at once, follow the process below:

  1. First, hit the “Esc” button to switch mode from insert to edit.
  2. Now type, “:%d”, and hit “Enter” to remove all the lines.

line/multi%202.png

How to delete a range of lines in Vim editor:

Vim also allows to select a range of lines and then delete them. The syntax is given below:

:[Starting Line Number],[Ending Line Number]d

For instance, if you want to delete lines from line number 5 to line number 7, then simply follow the below-mentioned process:

  1. Change the mode using the “Esc” key if it is in insert mode.
  2. Type “:5,7d” and hit “Enter”, line number 5,6, and 7 will be removed.

line/multi%203.png

If you want to delete all the lines before the current line, then use “:1,-1d“. For example, if you want to delete lines from line number 5 to all lines before this lines, then use the following method:

  1. Bring your cursor to line number 5.
  2. Press the “Esc” key and type “:1,-1d”, and then press “Enter”.

line/multi%204.png

To delete all line after line number 5, follow the below-mentioned process:

  1. Bring the cursor to line number 5.
  2. Press the “Esc” key and type “:+1,$d”, then press “Enter”, the lines that are below line number 5 will be removed.

line/multi%205.png

How to delete multiple lines in Vim editor:

In this method, we will learn the process of removing multiple consecutive lines. Simply follow the process:

  1. Bring the cursor to a line from where you want to start deleting.
  2. If you want to remove 4 consecutive lines, then simply type “4dd”.

line/multi%2010.png

In the above image, the cursor is on line 2, therefore lines 3,4,5, and 6 have been removed.

How to delete the line with a custom pattern in Vim editor:

Another feature of Vim editor is that it allows you to delete the line with a given condition, the syntax is given below:

:g /<WORD>/d

Replace “<WORD>” with the word of the line you want to delete. For example, if you want to delete lines with the word “Linux”, then use the following approach:

  1. Press the “Esc” key to change mode.
  2. Type “:g /linux/d”, then hit “Enter”.

The lines containing the “Linux” word will be deleted.

line/multi%206.png

Similarly, if you want to delete all lines, except lines containing the “Linux” word, then use

“:g! /linux/d”:

line/multi%207.png

To delete lines that begin with a specific letter, use

“:g /^T/d” ;

line/multi%208.png

To remove all blank lines in your text file or code, use

“:g /^$/d”:

line/multi%209.png

Conclusion:

Vim is one of the well-liked text editors among Linux users and developers. It is a free, well documented, and customizable text editor. In this guide, we learned how to edit text and code in Vim. We thoroughly discussed various methods and approaches to delete a line, multiple lines, and even lines with a specified word. Vim is a versatile editor that has a lot more features to uncover.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.