Linux Commands

Vi Delete Empty Unwanted Lines

There are several editors available in different distributions of the Linux systems, especially when you are working on the Ubuntu 20.04 Linux system. These editors come in handy when you need to edit or modify something within the file opened, i.e., add, remove, or update information. The “Vi/Vim” editor in Ubuntu 20.04 is very interactive, with various and simple modes to use within it. In today’s guide, we will be discussing the use of different Linux commands to remove extra empty lines from the file opened in the Vi/Vim editor.

Create File:

Get started with this guide by creating a new text file in the current working directory. Thus, we have tried the “touch” query of Linux to create one, i.e., “file.txt”. The output of the “ls” query has shown that the file “file.txt” is also listed in the home folder.

After this file’s creation, we tried to open it within the simple text editor of our system to add some simple text data to it. After adding 5 text lines and 5 empty files, we saved it.

We might think that the empty lines might not be output in the shell on execution, but that’s not the case. We have been running the “cat” query with the file name “file.txt” and have gotten the result of 5 text lines and 5 empty lines for this specific file.

Remove Current Line:

It’s time to open this file within the Vi/Vim editor of the Ubuntu 20.04 system to remove its empty lines. You can use the shown-below “vi” instruction along with the file name.

The Vi/Vim editor would open the file in the normal mode of execution. The editor has been displaying simple 5-line text data along with five empty lines, which are not highlighted by this editor. To highlight each line number, we need to set numbers for each.

You must be in the command mode of Vi/Vim editor to set the number for each text line in this file in the Vim editor. For this, you need to press “:” while you are in the normal mode. After that, you must use the “Set number” keyword instruction followed by the “Enter” key to set the numbers for all the lines mentioned in the file automatically in a numerical sequence.

The output of using the “Set number” instruction in the command mode of the Vi/Vim editor has been shown in the attached snap. All the lines now have their specific numbers sequentially assigned to them. We have placed the cursor at the 8th line of the file to delete its 8th, 9th, and 10th lines one by one while we are in the normal mode of the Vim editor displayed.

We have pressed “dd” in the normal mode of the Vim editor to remove its current line each time. So, using the “dd” instruction three times consecutively has made us remove the last three lines. On removal of the last 3 lines, the cursor automatically moves to the upward line, i.e., line 7.

To save this modification, you need to use the command mode by pressing the “:” character followed by the alphabet “x” and Enter key. If there is no problem, your file will be saved.

While saving this file, you can get the warning “Changing a readonly file” as shown.

To remove this warning, try using the “:x” command in the command mode of the Vi/Vim editor along with the exclamation mark “!” as shown. Press the Enter key to save this file using “:x!”.

If the above method doesn’t work for you to remove the warning, try using the chmod instruction to assign read, write, and execution rights to this file via the option “+rwx”.

After saving the file, we displayed its text content using the “cat” instruction once again. The output shows that the last three empty lines were successfully removed from the file.

Remove Multiple Lines:

You can also delete multiple empty lines from the opened file in the Vi/Vim editor. For this, place the cursor at the line from where you want to remove the lines, i.e., 5th line.

Go to the command mode using the “:” character and add the number of lines to be deleted with the “dd” instruction with the Enter key, i.e., 3dd to remove 3 consecutive lines at once.

The last 3 lines are removed, and the cursor has been moved to the upward line.

Remove Range of Lines:

To remove multiple empty lines, we can also set the range. The range consists of the start and end of lines to be deleted. Let’s say we want to delete all the lines from 4th to 7th in the below shown 7-line file in Vim. So, we have used the command mode to set the range. This range has been started with line 4 and ends with line 7, along with the “d” character.

On execution of this range command, we are left with only the first 3 lines of this file.

Remove All Lines:

To remove all the lines from the file opened in the Vi/Vim editor, it comes with the “%” character usage with the “d” character in the command mode as we did below.

The file will become empty after the execution of the “%d” instruction.

Remove Last Line:

If you want to delete the last most empty line of this file, you can do so by using the “$” character with the “d” character in the command mode of the Vim editor.

The use of the “:$d” instruction has removed the 7th line from this specific file.

You can try out the special character dot “.” With the “d” command in command mode to delete the current line from the file, i.e., line 06.

The current empty line “6” has been removed.


This guide discusses the best of the best examples to display how we can remove empty and unwanted lines along with the text lines from the file. For this, we have started this guide with the creation of a simple file and opening it in the Vi/Vim editor. After this, we discussed how we can remove a single current line, multiple consecutive lines, some range of lines, the last line, and all the lines from the file.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.