English
+370 5 205 5502 sales@monovm.com

How to Delete lines in Vim/Vi?

Vim is one of the best and most popular editors in the Linux OS. In this tutorial, we learned how to use Vim to edit text and code.

11 Jan, 22 by Susith Nonis 3 min Read

List of content you will read in this article:

Vim may be used to create, modify text and computer program files for different reasons. When processing text files in the vim editor, you frequently have to remove one or more lines, and it allows you to easily modify your files, as does any other text editor. This article concentrates on editing and deleting lines in the Vim editor from your files.

dd is the Vim command for deleting a line.

The following are detailed steps for deleting a line:

  1. To return to normal mode, use the Esc key.
  2. Place your pointer on the line you'd like to remove.
  3. To delete a single line, type dd and press the Enter button.

Numerous presses of the dd key will erase multiple lines.

  1. To return to normal mode, use the Esc key.
  2. To erase a term, place your cursor at the beginning of the term you want to remove.
  3. Make the following keystrokes: dw

Prefix the dd command and mention the number of lines to be erased to delete several lines at once. To remove five lines, for example, perform the following:

  1. To return to regular mode, use the Esc key.
  2. Place your cursor on the first line you'd like to remove.
  3. To remove the following six lines, type 6dd and press Enter.
  1. In Vim, press the Esc key to return to normal mode.
  2. When executing the following command, replace [from] and [to] with the line numbers where you want the array to start and end, respectively.

:[from],[to]d

For example, if you wanted to erase lines 3,4,5,6 and 7, you'd type:

:3,7d

  1. You may use either the percent sign (which indicates all lines) or the 1,$ range to remove all lines:
  2. To return to normal mode, use the Esc key.
  3. To remove all the lines, type % d, and press Enter.

The following is the syntax for removing several lines based on a pattern:

:g/<pattern>/d

The delete command (d) is told to remove all lines bearing the <pattern> by the global command (g).

For lines that do not suit the pattern, add an exclamation point (!) before the pattern:

:g!/<pattern>/d

1. To return to normal mode, use the Esc key. and remove all blank lines.

2. After that, type the following:

:g/^$/d

Vim is one of the best and most popular editors in the Linux OS. It's a text editor that's free, well-documented, and configurable. In this tutorial, we learned how to use Vim to edit text and code. We went through different ways and tactics for deleting a single line, many lines, and even lines containing a specific phrase. Vim is a powerful text editor with many more capabilities to discover.

People also read: 

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I veture into the world of Technology I try to share what I know in the simplest way possible. Not a fan of coffee, a sweet addict and a self accredited 'master chef'.