How to Delete Lines in Vim/Nvim with GIF Examples

Deleting lines in Vim editor is a fundamental task system administrators commonly face. However, you can delete lines in any command line editor like Nano, but Vim offers to delete lines and multiples line in the entire document effectively.

In this blog post, I’ll discuss the basic deletion of lines to advance ways to delete lines in the Vim editor.

Basic Delete Line Operations

Vim offers several ways to delete lines in documents. You can delete a single line, delete multiple lines, or a range of lines in the Vim editor. In this section, we will learn the easy way to delete lines in the Vim editor.

1. Deleting the Current Line with dd

The easiest way to delete lines in Vim is to use the dd command in the normal mode/command mode. To delete a line, put your cursor on this line, then press the dd on the keyboard. Then the entire line will be gone, and the rest lines will shift up.

I have copied the Grub configuration file for example purposes. Let’s say you want to delete the second line. To delete the second line, put your cursor on the second line and press dd on the keyboard.

GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Arch"
GRUB_CMDLINE_LINUX_DEFAULT="loglevel=3 quiet"
GRUB_CMDLINE_LINUX=""

After performing dd on the second line:

GRUB_DEFAULT=0
GRUB_DISTRIBUTOR="Arch"
GRUB_CMDLINE_LINUX_DEFAULT="loglevel=3 quiet"
GRUB_CMDLINE_LINUX=""

2. Using :delete Command

Additionally, you can delete lines in Vim with the delete command in Vim normal mode. The:delete command has more functionality compared to dd command in the previous example. To delete any line in Vim, first put the cursor on the line and enter :delete command to delete the line.

:delete
Using :delete Command

3. Deleting Multiple Lines with the delete command

The :delete command in the previous example also supports deleting multiple lines simultaneously. You can specify the range of lines you want to delete. You can enter:set number command in the Vim command to enable the line number. Then you can specify the range you want to delete.

In :delete command, you can specify the range in two ways. First, you can select the range with the line number. For example, you want to delete lines 14 to 15. Then you will use the delete command in the following way

:4,15delete
Deleting Multiple Lines

Another way of deleting multiple lines with :delete command is to specify how many numbers you want to delete from your current cursor position. Let’s say you want to delete the following 3 lines from your current position. Then you would use the following command

:delete3

This command will delete the following 3 lines from the configuration files.

Deleting Multiple Lines

Deleting Lines with Conditions

Alternatively, you can delete lines in Vim only after certain conditions are matched and selectively delete lines after checking patterns by combining regular expression (regex) and Vim commands.

1. Conditional Deletion using :g

The :g command combined with the d command in Vim allows you to search for a word in lines, if the word is found in any line, then the lines will be deleted. So, Vim will only delete the lines that contain the specific word.

Let’s say you want to delete lines that contain GRUB words in the lines. Then you can use the following command.

:g/GRUB/d

This command will delete the lines that contain GRUB.

Conditional Deletion using :g

2. Conditional Deletion based on Line Content

You can also delete any lines based on their content using a pattern-matching combination with the delete command in Vim. For example, you want to delete lines that have a specific word in the ending line. So Vim will find the lines that end with your specified word and delete this line.

Let’s say you want to delete all the lines that end with the Console word. You can use the following command

:g/Console$/d

This command will display all the lines that end with Console.

Conditional Deletion

3. Deleting Lines based on Pattern Matching (Regex)

You can also delete lines after matching a specific pattern. To do this, you need to combine regular expressions with the Vim command to match complex patterns and delete the lines.

For example, you want to delete all the lines that start with a specific word. You can use regular expressions to match the pattern and delete the lines. Let’s say you want to delete the lines that start with GRUB. Then you can use the following command.

:g/^GRUB/d

This command will display all the lines that start with GRUB

Conditional Deletion regex

Deleting Blank Lines and Whitespace

We often need to make our configuration files and code neat and clean. The configuration or code you work with usually contains many blank lines and whitespace. Deleting the blank space and whitespace is a time-consuming process. You can utilize Vim to delete blank lines and whitespace from your documents.

1. Deleting Blank Lines

You can easily remove blank lines in Vim by using pattern matching that we have used in the previous examples. In this case, you have to specify a pattern for blank lines.

Use the following command to delete all the blank lines from the configuration file.

:g/^$/d
Deleting Blank Lines

2. Removing Leading and Trailing Whitespace

Unwanted whitespace can often cause problems with your configuration and code. Unwanted, Whitespace also looks ugly. You may want to delete them all. Vim allows you to search for whitespace and delete the whitespaces.

To delete leading whitespace, use the following command

:%s/^\s\+//

This command will delete whitespaces from the entire documents

delete Whitespace

To delete trailing whitespace, use the following command

:%s/\s\+$//

3. Deleting Blank Lines and Whitespace Combined

You can combine commands to delete blank lines and excess whitespace in one operation. This ensures a more concise and clean document.

You can combine the previous two examples to delete blank lines and whitespaces simultaneously.

:g/^\s*$/d

Conclusion

Vim supports an effective way to delete lines in the Vim editor. Following this blog, you can delete single or multiple lines, specify the range to delete lines, delete whitespaces, and delete blank lines.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

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