How to Delete Line in Vim on Linux?

Photo of author

By admin

Vim, earlier called Vi, stands for “Vi IMproved”, is a multi-talented content editor that is viable with all UNIX-based operating systems like Linux and macOS. Vim can be utilized for different purposes, for example, making, or altering text, and additionally, PC program records.

You frequently need to delete/erase at least one line while altering text records in Vim proof-reader. Unlike another similar kind of word processor, Vim permits you to change your documents proficiently. This blog post will help you delete a single line, all lines, multiple lines, a range of lines, lines in a pattern, and much more using the most proficient method in the Vim editor.

You can find various methods to erase lines in Vim. This famous Linux content editor permits you to eliminate multiple or determined examples from a record utilizing a single command. In this post, you will learn to erase lines in Vim using various commands.

In case you don’t have a Vim editor, spare time to install one.

Let’s have a look at some essential ways to delete lines in the Vim editor on Linux.

  1. How to Use Vim Editor to Delete a single line.
  2. How to use Vim Editor to Delete all lines.
  3. How to use Vim Editor to Delete multiple lines.
  4. How to use Vim Editor to Delete a range of lines.
  5. How to Use Vim Editor to Delete lines by a given pattern.

How to Use Vim Editor to Delete a Single Line

Vim Editor is a beautiful text editor that helps professionals to delete or edit a file on Linux. If you want to delete a single line in a file, follow the command mentioned below:

Before moving on to the command to delete a line, we would like to mention the two modes in vim — Command Mode and Insert mode.

When you are working in the command mode, you can edit the file, delete the text, etc.

In the insert mode, as the name suggests, you can insert text as well.

If you want to switch from command mode to insert mode, you need to type the command “a/A/i/I/o/O

To switch from insert mode to command mode, you need to press the Esc key.

Now to delete a single line in a file, use the command “dd” or “:d”. Here are the steps to delete a single line.

  • To switch to normal mode you have to press the Esc key.
  • Further, bring your cursor to the specific line that you need to erase or delete from your file.
  • Type the command “dd” or “: d” and press the Enter key to remove the line from the file.
  • If you want to delete multiple lines from the file, continue pressing the command “dd” or “: d”.

How to Use Vim Editor to Delete All Lines

When you want to delete all lines, you can either use the command % representing all lines or use 1,$d to describe the range of lines (starting from the first line to the last) you wish to delete. To erase all lines from the file, follow these simple steps:

  • Press the Esc key and switch to the normal mode.
  • Further, to all the lines at once type and enter the following command “:%d”, and press “Enter”.
  • Entering this command will help you to delete all lines from the file.

After finishing the process to delete the lines from files, the VIM Editor will notify you with a notification of no lines available. For example, you have some lines already written in your file and your file has lines 1 to 5. To delete all lines from the file, you will use the command “: %d” and the editor will erase all lines for you.

There is another way to delete all lines from the file. You can use the command “:1, $d”. This command tells the editor to delete all lines starting from the first line to the end of the file.

How to Use Vim Editor to Delete Multiple Lines

Now that we have learned to delete one line and all lines in a file, we will delete multiple lines in a file.

How is this different from deleting all lines? While using the command to delete all lines, we are deleting all lines from the file, but when we use the command to delete multiple lines from the file, we aren’t removing all lines but a few. Let’s see how to remove multiple lines that are consecutive to each other. Make sure, you use this command to delete lines that are following each other. Follow the below-mentioned steps to delete multiple lines:

  • Press the Esc key and switch to the normal mode.
  • Then move your cursor to the first line from where you want to start deleting.
  • Using command “dd”, mention the number of lines that you want to delete. Taking an example of a file that has multiple lines. To delete the specific line you can use the command “dd” and mention the line that you want to delete, in case you don’t want to delete a single line or all lines. For instance, if you want to delete 5 lines from a file, use the command “5dd”.

How to Use Vim Editor to Delete a Range of Lines

Now that you have mastered deleting a line, all lines, multiple lines, let’s see how a user can delete a range of lines. By range, we mean a particular starting and finishing point.

The command to delete a range of lines using Vim Editor is:

:[start from line],[end on this line]d

Let’s take an example to make it more simple: If you want to delete a range of lines starting line 2 and until line 5, you need to follow the below-mentioned steps:

  • Press the Esc key and switch to the normal mode.
  • The next step is to write the command: 2,5d and press the Enter key to delete the particular range of lines 2,3,4, and 5.

Some characters can be used to clarify the range of lines.

You can use

. (dot) – to represent the current line.

$ – to represent the last line.

% – to represent All lines.

Some examples to make it clear:

:.,$d – means continue deleting the current line until the end of the file.

:.,1d – means continue deleting the current line to the start of the file.

10,$d – means continue deleting from the 10th line to the end of the file.

How to Use Vim Editor to Delete Lines by a Given Pattern

Apart from all other features, the Vim editor is also capable of deleting lines in a pattern. If you want to delete lines containing a particular word, for example, “Star”, you can do that using this feature. Here are simple steps to delete lines containing the expression “Star”:

  • Press the Esc key and switch to the normal mode.
  • The next step is to write “:g /word that you want to highlight (in this case it will be Star)/d”, then press “Enter”.

This command will delete all lines that contain the word “Star”.

You can also use this feature to delete all lines not containing any particular word or pattern. For example, if you want to delete all lines that do not have the word “Star”, you can follow the below-mentioned steps:

  • Press the Esc key and switch to the normal mode.
  • The next step is to write “:g! /word that you want to highlight (in this case it will be Linux)/d”, then press “Enter”. (Note that the user needs to add a “!” after the command “:g” to clarify that he or she wants to remove all lines except the lines containing the word or pattern following it.)

This command will delete all lines that do not contain the word “Star”.

Conclusion

It can be difficult for beginners to get started with Vim, so we did some research work and came up with this post to make things easier for them. A lot of time, people need a reference to begin their task. For all engineers and content editors looking for a magical Text Editor on Linux, Vim can be a blessing.

For Linux clients and engineers, Vim editor is considered the most preferable editor. It’s a free, much-reported, and adaptable word processor. In this post, we figured out how to delete a single line, multiple lines, all lines, a range of lines, and lines containing (not containing) specific patterns in Vim. Vim is a flexible proofreader that has much more highlights to reveal.

In the wake of perusing this article, you currently have different choices for erasing lines and words in Vim. Understanding these commands can help you accelerate working with Vim, as you don’t need to physically eliminate undesirable substances.

Leave a Comment