How to Delete Lines in Vim or Vi

Written by: Bobbin Zachariah   |   Last updated: October 28, 2022

UNIX-based operating systems offer various text editors. Working with text files on the command line is challenging for most beginners. Luckily, you don't have to hassle any longer, especially when you must delete multiple or specific lines in a text file.

With a text editor like vim or vi, you can utilize its normal mode or command mode to edit a file quickly, including deleting lines. Besides, there are plenty of options that you can use, provided you know how.

This guide covers all details on how to delete lines in vi or vim editor.

Delete Current line

The dd command is used in the vim editor to delete a line.

To delete the current line, ensure you are in normal mode. If not, press the Esc key to switch to normal mode.

Next, delete the current line by pressing the dd command or the D on your keyboard. You will note the current line will automatically get deleted. Ensure you only press dd once to avoid deleting multiple lines.

Delete Specific Line

To delete a specific line in the vim editor, you must switch to command mode. Suppose we wanted to delete line 4, enter the command, :4d, and then press the enter key.

Alternatively, you can use the normal mode. Press the Esc key, then move your cursor to the specific line and delete it by pressing dd on your keyboard.

Delete Multiple Lines

When you want to delete multiple lines, switch to the normal mode, then enter the number of lines you want to delete, followed by the dd command.

For instance, to delete the next 4 lines from the current line, press 4dd, then press the enter key.

Delete a Range of Lines

Command mode allows you to specify various wildcard characters for deleting lines in a range.

  1. Dot (.) - It represents the current line in vim.
  2. Dollar ($) - It represents the end of the file.

Let's have some examples.

To delete all lines between the current line till the fifth line, the command would be :.,5d

Alternatively, you could specify the starting point and the last line. The command :3,5d would delete lines 3, 4, and 5.

To delete all lines in the vim editor starting from the current one, the command would be :.,$d

Delete All Lines

You can clear all the lines in the vim editor in command mode by pressing :$d and the enter key.

This option is helpful when you want to start writing afresh and have multiple lines that you no longer need.

Delete Lines By Pattern

The vim editor also deletes specified patterns. It could be a regex expression or a literal match. Either way, you can delete lines that match the specified pattern using the :g/<pattern>/d syntax.

Let's have some examples.

If you want to delete all lines that contain a given keyword, such as "nix", you can specify the command as :g/nix/d and press the enter key.

Similarly, add the exclamation mark if you want to remove all the lines that don't have the specified keyword. Our new command would be :g!/nix/d

If working with a script and want to remove all commented lines, use the command :g/^$/d

Delete Specific Number of Lines

When you want to delete lines in vim, you can specify the number of lines you want to delete.

For instance, if you want to delete 6 lines in vim, switch to normal mode and type 6dd, then press the enter key. The next 6 lines will be deleted.

Delete All Empty Lines

All blank lines in vim can be deleted using the pattern option. In this case, a blank line is represented by the dollar sign. Use this pattern to remove all blank lines :g/^$/d, then press the enter key.

To delete the empty lines containing zero or more whitespace, tweak the above command and add \s*. Our new command will be :g/^\s*$/d

Delete Lines Above

You may want to delete one or more lines above your current position when in a specific position. Switch to normal mode, then place the cursor where you want to start deleting all the lines above by pressing dgg and hitting the enter key.

Delete Lines Below

From the current position of your cursor in vim, you can delete all the lines till the end of the file. Use the command :.,$d

Vim Delete to End of Line

The end of the line is specified as d$. Therefore, to delete words in a line in vim, move the cursor to where you want to start deleting words.

Press the Esc key to enter normal mode, then press d$ to delete all the words till the end of the line.

Conclusion

The vim or vi editor is not a complicated text editor. At first, the many options may seem confusing but rest assured, you will quickly get a grip of it if you keep practicing.

Try vim editor, and feel free to leave a comment about your experience using the text editor.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment