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:

<img alt="line/1 copy.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-120copy-png.png" data-lazy- height="146" src="data:image/svg xml,” width=”858″>

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:

<img alt="line/multi 1.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi201-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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.

<img alt="line/multi 2.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi202-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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.

<img alt="line/multi 3.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi203-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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

<img alt="line/multi 4.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi204-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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.

<img alt="line/multi 5.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi205-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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

<img alt="line/multi 10.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi2010-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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:

Replace “” 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.

<img alt="line/multi 6.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi206-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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

<img alt="line/multi 7.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi207-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

To delete lines that begin with a specific letter, use

<img alt="line/multi 8.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi208-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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

<img alt="line/multi 9.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/line-multi209-png.png" data-lazy- height="720" src="data:image/svg xml,” width=”1280″>

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

<img alt="Sam U" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/CD836339FE0346CD9A43623F995D6B46-150×150.png6030bf8aaa628.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.