How to Delete all lines in vim? Safe ways

February 28, 2022

Vim is a multipurpose text editor compatible with UNIX-based operating systems like Linux and macOS. You can use it to create or edit program files.
Indeed it will allow you to Delete all lines. Unlike other editors, you can use it to edit files very efficiently.

One of the most-known Vim tricks is clearing or deleting all text or lines in a file. Although this is not a frequently used operation, it will help you deal with some cases faster, especially when you need to delete the entire file content.

So, in this tutorial, I will show you how to delete all lines using the vim editor.

Vim delete all lines normal mode.

The first option is to remove, clear, or delete all lines in a file in the normal mode (note that Vim starts in “normal” mode by default). Immediately after opening a file, type “gg” to move the cursor to the first line of the file, assuming it is not already there. Then type dG to delete all the lines of text on it.

Vim delete all lines insert mode.

If you are having trouble deciding whether to use insert mode or delete all lines, you can easily find the right command in Vim.

Follow the steps below to delete all lines at once.

  1. First, to switch from insert to editor hit the ” Esc” button.
  2. To remove all lines, type “:%d” and hit ” Enter.”

Vim delete all lines command mode.

Alternatively, you can clear all text lines in Vi/Vim in command mode by running the following command.


Vim Delete All Lines With Pattern

This section will introduce you to vim's dd, ex, and g commands. These commands will help you delete all lines containing a pattern. So, To get started, type %d to delete all lines with a particular pattern. To return to the normal mode, press Esc. Once you are back in normal mode, type %d again to remove all lines bearing a pattern.

vim dd

If you have a pattern in your text file and want to delete multiple lines simultaneously, you can do it with vi. Vim has a dd command to delete all lines beginning from the cursor. You can type dd to delete all lines beginning with the current line. You can also use this command to delete the last ten text lines from the cursor's bottom.

It is a helpful command for deleting multiple lines in a single go.
To use the dd command to delete multiple lines, you must position your cursor at the beginning of the line and then prefix it with a line number. Vim has additional resources like whitespace, vim text objects, and vimscript, which are available to edit your text. Also, you can use vim dd to delete a range of lines into a named register.

vim ex

You can use the vim ex delete all lines with pattern command to remove all lines containing the specified word. This command is very useful when removing multiple lines with the same pattern. The pattern is used to identify the word that matches the pattern. For example, if you want to delete a string, you can enter %d to remove all lines with a matching word or expression. Then, you can press the Esc key to return to the normal mode.
There are many ways to use this command, but one of the most popular and useful is to specify a range. For instance, $-5 would be used to select the fifth line from the bottom. And +3 would be used to select three lines below the current line. But the range will vary based on the command, so use it only when needed.

vim g

The command 'vim g delete all lines with pattern' acts on all lines that match the specified pattern. It does this by marking the lines with the pattern. Then, execute the 'd' command on those marked lines. Note: vim g delete all lines with pattern cannot remove trailing whitespace, which is due to how the 'd' command works. It will delete every line containing the pattern.

The global command operates on the same pattern system as the substitute command, so when you specify '.,3,5d', it will delete all lines with the same pattern. You can also use the '%d' command to delete all lines with patterns in the file. Using this command, you can quickly delete a range of lines by typing in a pattern. A single-line bracket specifies the range, so if the pattern begins on line one, it will be deleted.


Vim is a popular text editor among Linux developers and users. It’s a free text editor that is well-documented and customizable. This guide was shown you how to delete all lines in Vim. We discussed many modes to delete all lines, normal mode, insert mode, and command mode.

The right decision!
If you're ready to make an impact in your business, it's time to get started now! Discover our offers and make the right decision!
Get Started Now!
payment gateway method for lgvps company
payment gateway method for lgvps company
Stripe paypal method for lgvps
A BeinCart LLC Company
Copyright © 2022 BeinCart LLC | Designed by LGVPS Team