How to Delete Lines in Vim / Vi

Introduction

Most Linux distributions and macOS come with Vim or its predecessor Vi preinstalled. If you're a system administrator or just a normal Linux user, knowing the basics of Vim is essential.

When working with text files, you'll almost always need to delete one or more lines.

In this tutorial, you will learn how to delete lines in Vim/Vi. We will also address a few FAQs on how to delete lines in Vim/Vi.

Deleting a Line

dd is the Vim command for deleting a line.

Following are the steps for deleting a line:

  1. Press the Esc key to return to normal mode.
  2. Put your pointer on the line you want to delete.
  3. Type dd and press Enter to remove a line.

Multiple presses of the dd key will remove multiple lines.

Deleting Multiple Lines

Prefix the dd command with the number of lines to be erased, to delete many lines at once. To eliminate five lines, for example, perform the following:

  1. To return to regular mode, press the Esc key.
  2. Place your cursor on the first line you'd like to remove.
  3. To erase the next five lines, type 5dd and press Enter.

Delete a Range of Lines

The following is the syntax for removing a range of lines:

:[start],[end]d

To eliminate lines 3 to 5, for example, do the following:

  1. To return to regular mode, press the Esc key.
  2. To delete the lines, type :3,5d and press Enter.

Specify the range using the following characters:

  • The current line is denoted by a . (dot).
  • $ denotes the last line,
  • % denotes all lines.

Listed below are a few examples:

  • :.,$d - Goes from the current line to the file's end.
  • :.,1d - From the current line to the file's beginning.
  • 10,$d - From the tenth to the last line of the file.

Delete All Lines

You can use either the percent sign (which indicates all lines) or the 1,$ range to erase all lines:

  • To return to regular mode, press the Esc key.
  • To remove all the lines, type %d and press Enter.

Deleting Lines Containing a Pattern

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

:g/<pattern>/d

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

Add an exclamation mark (!) before the pattern to match the lines that don't match the pattern:

:g!/<pattern>/d

A literal match or a regular expression can be used as the pattern. Here are a few examples:

  • g/foo/d - Remove any lines that contain the string "foo." It also removes lines if the word "foo" appears in larger terms, such as "football."
  • :g!/foo/d - Remove any lines that do not include the string "foo."
  • :g/#/d - In a Bash script, remove all comments. The ^# denotes that each line begins with the letter #.
  • g/^$/d - Get rid of any blank lines. All empty lines are matched by the ^$.
  • :g/s*$/d - Remove all blank lines. This command, unlike the previous one, also removes blank lines with zero or more whitespace characters (\s*).

FAQs on How to Delete Lines in Vim/Vi

Can I delete multiple consecutive lines at once in Vim/Vi? 

Yes, you can delete multiple consecutive lines by specifying the number of lines to delete followed by the dd command. For example, 2dd will delete the current line and the line below it.

How can I delete the current line and remain in insert mode in Vim/Vi? 

Press dd in normal mode, and Vim/Vi will delete the current line and keep you in insert mode.

Is it possible to delete lines before or after the current line in Vim/Vi? 

Yes, use the command ndd to delete the current line and n lines after it, or ndk to delete the current line and n lines before it.

Can I delete lines based on specific criteria, such as containing certain text? 

Yes, you can use commands like :g/pattern/d to delete lines that match a specific pattern.

How can I delete all lines in a file at once in Vim/Vi?

Use the command :%d in normal mode to delete all lines in the current file.

Can I delete lines without affecting the contents of the system clipboard? 

Yes, you can delete lines without affecting the system clipboard by using the command "_dd instead of dd

Can I delete empty lines only in Vim/Vi? 

Yes, the command :g/^$/d can be used to delete all empty lines in the file.

Conclusion

In this tutorial, we have shown you how to delete lines in Vim.

If you're new to Vim, check out the Open Vim site, which has an interactive tutorial to help you get started.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.