how to delete lines in vi editor

How to Delete Lines in VI Editor

Sometimes while working with vi editor you may need to remove one or multiple lines in your file. In this article we will look at how to delete lines in vi editor. We will look at how to delete single line in vi editor, remove multiple lines, delete a range of lines and even remove lines matching a pattern.


How to Delete Lines in VI Editor

We will look at different use cases to delete lines.


Delete single line

If you want to delete a single line vi editor

  • Press Esc key to go to normal mode
  • Move your cursor to the line you want to delete
  • Press dd (d twice) and the line will be deleted
  • Save & exit the file to save changes

If you press dd multiple times, then vi will delete one line after another.

Also read : How to Install NGINX with GeoIP module


Delete multiple lines

To delete multiple lines you need to append dd to the number of consecutive lines you want to delete. For example, you need to type 5dd to delete 5 consecutive lines from your cursor position.

  • Press Esc key to go to normal mode
  • Move your cursor to the first line from where you want to delete multiple lines
  • Press 5dd to the delete 5 consecutive lines

Also read : How to Run multiple websites on Apache server


Delete Range of Lines

If you know the line numbers of the lines to be deleted, you can directly delete them without moving your cursor. Here is the syntax to delete lines using their line numbers.

:[start],[end]d

For example, if you want to delete lines 2-5,

  • Press Esc to go to normal mode
  • Type :2,5d and hit enter

Here are a few shortcuts to help you delete multiple lines.

  • dot(.) – present line
  • $ – last line of file
  • % – all lines

Here is how you can use them:

  • :.,d – present line to last line of file
  • :.,1d – present line to the 1st line of file
  • 5,$d – line number 5 to last line of file

Also read : How to Set Environment Variable in Linux


Delete All Lines

Sometimes you may want to remove all lines of your file. If you want to delete all lines in vi editor,

  • Press Esc key to enter normal mode
  • Enter %d or :1,$d

Also read : How to Install Webmin in Ubuntu


Delete lines matching pattern

If you want to delete all lines that match a pattern then you need to enter the pattern between g (global) and d (delete). g tells vi editor to look for a pattern and d tells it to delete those lines.

:g/<pattern>/d

Similarly, if you want to delete lines that do not match a pattern, use g! before the pattern

:g!/<pattern>/d

For example, if you want to delete all lines containing the pattern ‘test’,

  • Press Esc key to go to normal mode
  • Type :g/test/d

You can use regular expression characters in your pattern. For example, you can use

  • ^ indicates beginning of the lines. For example, ^# means all lines beginning with # character
  • $ indicates end of file. For example, #$ indicates all lines ending with # character

Also read :
How to Configure Iptables in Ubuntu
How to Search in Vi Editor


Leave a Reply

Your email address will not be published. Required fields are marked *