how to delete lines in vi or vim editor in linux

If you use vi or vim editor to edit text files, then you should be aware of the different text manipulation commands that exist in the editor. One of the common actions you will perform is the deletion of a single line or the deletion of multiple lines of text from with in the editor.

Let’s see the various ways you can delete a line in the vim editor. It goes without saying that you will execute all of the commands mentioned below in the command mode. In vim or vi, you can enter the command mode at any time by hitting enter.

delete a single line in vi or vim editor

The deletion of a single line is probably the easiest. The command to perform the deletion is dd, that is pressing the letter ‘d’ in quick succession twice. It is just like double clicking but with the key.

  1. Go to the line that you want to delete in the editor
  2. Press the ESC key. This is to make sure that you are in the command mode.
  3. Press dd to delete the line

Hitting dd multiple times will delete multiple lines, albeit one at a time. As the shortcut to delete a single line is so easy, I sometimes use it to delete multiple lines, one after the other. It works pretty well if you are deleting a small number of lines, but if you want to delete a larger number of lines then there are other ways to do it.

delete multiple lines in vi editor

If you know how many lines you want to delete ahead of time, then deleting multiple lines is just as easy as deleting a single line. You will prefix the dd command with the number of lines you want to remove.

  1. Go to the first line that you want to delete
  2. Press the ESC key
  3. Press the number of lines you want to delete, followed by dd (eg. 5dd or 9dd)

In this case, the lines that you want to remove should be in succession or consecutive and the cursor should be placed on the first line of the block that you want to delete. Usually this is the case the case and you can easily do it with the dd command.

delete lines in a range in vi or vim editor

You can delete lines with in a range, if you know the start and end line numbers. This is a similar case to the one we mentioned in the previous section. The general syntax for deleting a range of lines is:

:[start number],[end number]d

So, to delete all lines between the line number 111 and 234, you will

  1. Hit ESC key
  2. type :111,234d
  3. Hit Enter

This is useful when you want to delete a large number of lines, but you don’t have to traverse down to the exact line and calculate the number of lines to delete.

delete all lines in vi or vim editor

You can use the same syntax or method as above to delete all lines in the file when it is open in the vim editor. There are other ways to empty or truncate a file if you want to, but if you are already in the editor, then this will work just as well.

  1. Hit ESC key
  2. type :1,$d
  3. Hit Enter

The $ character denotes the last line in the file. So the above command will delete lines starting with the first line and ending with the last line.

delete all lines before the current line

Another variation of the range is to delete the lines before the cursor or the current line in the vi or vim editor. The dot (.) character denotes the current line in the editor.

  1. Hit ESC key
  2. type :.,1d
  3. Hit Enter

The above command will remove all lines starting with the first line till the line before the current line. The current line is defined as the line where the cursor is. Another way to do it is to use :1,.-1d

delete all lines after the current line

This is a variation of the previous method, To delete lines after the current line, you can use the dot (.) to denote the current line and dollar ($) to denote the last line.

  1. Hit ESC key
  2. type :.+1,$d
  3. Hit Enter

Here you are adding one to the current line and $ denotes the last line of the content in the editor. So this will delete all lines after the current line. To delete all lines including the current line, do not add the 1.

delete lines that match a pattern in vi or vim editor

You can also do a search and delete, if you want to delete lines that contain certain words or character sequences. So, if you want to delete all lines in the file that contain the word ‘saloon‘, then

  1. Hit ESC key
  2. type :g/saloon/d
  3. Hit Enter

The search is case-sensitive by default and this will delete all the lines that match the search pattern specified in the command.

delete lines that do not match the pattern

If you want to delete that do not match a specific pattern, then you will need to negate the match before deleting. So, to delete lines that do not have the word ‘saloon’, you will

  1. Hit ESC key
  2. type :v/saloon/d
  3. Hit Enter

Using v instead of g will negate the pattern. You can use complicated regular expression to match the lines.

delete all blank lines in a file

To delete all blank lines in a file, you can use a variation of the pattern matching. The ^ (caret) denotes the start of a line and the $ denotes the end of the line. Using these two characters together denotes an empty or blank line. This does not match against lines that contain blanks or space character in them. You will need to use a different regular expression to match those lines.

  1. Hit ESC key
  2. type :g/^$/d
  3. Hit Enter

Pattern matching in vi editor is a very powerful tool. If you make yourself familiar with the pattern matching commands in vi, then you can use it for all the various different text manipulation that you will come across.

I am a big proponent of always hitting the ESC key before entering the command, just to make sure that you are in the command mode. If you already know that you are in the command mode, then this step is not necessary.