How to Delete Lines in Vim / Vi

Vim comes out as an outstanding tool that helps to configure files and editing text mostly under Linux. One quirk of Vim that many cannot acknowledge is its capability to delete or erase complete lines in a text or file. Nothing complex about Vim but learning it is definitely useful.

Like every other work, deleting a line in Vim too has various methods. Vim, the widely known editor for text in Linux, can undo one or many patterns in a single command.

This tutorial will help you get knowledge on deleting lines by range or pattern by various options provided by Vim.

Various ways to delete a line in Vim

How to delete a single line?

  • Press on the Esc key and rapidly move along different files.
  • Move the cursor towards your line of choice for deleting.
  • If you want to delete a line, you need to press one of the following commands,
DD
D

Deleting a single word

  • Pressing Esc helps you enter the mode of command.
  • Next step involves the pointer being placed at the start of the very word.
  • Press down on the key that instant.

Steps for deleting multiple rows?

  • Start by pressing the Esc key.
  • Next step involves moving pointer to the line that’s needed to get deleted.
  • One use of one of the below mentioned commands the line will get deleted
[#]d d {# stands for no. of lines}
D-d-d

Method for deleting series’ of lines

  • Start by using the Esc key to enter a normal mode in Vim.
  • The following command will be brought to use with the beginning of the line’s range to the end of the line’s spectrum.

Example for reference is, deleting lines 3, 4, 5, and 6 by using the command: 2,6d.

Methodology for – delete all rows.

  • Firstly you press the Esc key.
  • Finally clear out everything inside the file by command, %d.
  • Once this command is executed, Vim will pop up, “No lines in buffer”.

Deleting the line present at the end of the file

Firstly the person needs to be moved to the line that needs deletion.
Entering command mode is followed by pressing of the Esc key.
The last step is to getting deleted every line except the ones you want to keep. The command needed: $d.

Deleting the line present at the beginning of the file

The first thing is to position the cursor properly on the line that needs deletion.
I am following with pressing the escape key.
Finally using the command delete the line at the beginning of the file.

Steps for deleting template-based lines

Vim allows you to get the lines with the coincidence of patterns. Many ways are available for deleting template-based lines. There are several ways to delete rows based on a template.

Deleting line laced with the specific word

  • Bring to use the Esc key and enter yourself into the mode of normality in the text.
  • You can delete the line with a specific word using, r / word/.

Getting lines lacking specific words deleted

  • Esc key will help you enter in command mode.
  • Use a particular command, r!/word/. To delete like lacking a specific word.

The process to delete the empty lines.

  • Blank lines can be deleted using the Esc key and switching to the normal mode.
  • Following above, enter the mentioned command will do the trick - r/^$/d

When does vim delete every line?

You get to delete a Vim/V line using: 1,$d. When you decide to delete all lines in vi go for: 1, here $ is symbolic of the line which is next in the command. Once a line starts with:g/^$/d it has the potential to delete all empty lines. ^$ present is the resemblance of the empty lines present. The command: g/^s/\*$/d, is capable of deleting every empty line present.

Conclusion

Once you have gone through the above article and have come to acknowledge the analysis well, you will have yourself many choices as to how you can delete a line or word on Vim.
Once you are well aware of Vim’s different commands, you will come to realise how time-efficient Vim will turn out to be.