You can delete a line in Vim with
dd i.e. pressing the
d key twice. But what if you want to delete all the lines in Vim?
You cannot do Ctrl+A and use Del key like you do in a regular text editor. That's not how Vim works.
There is no straightforward keyboard shortcut for this. However, you can use the
dG keys if you are at the beginning of the file.
Deleting all lines in Vim
Here are the steps:
- Make sure you are in the command mode in Vim by pressing the
- You need to be at the beginning of the file. Press
ggfor that. This means pressing the
gkey in quick succession.
- Now press
dG. This means pressing the key
dand then hold
Gis uppercase and you'll have to use shift key here the same way you type G.
dG is what you need to do for deleting all the lines of the file in Vim.
So, how does this
dG command works? To be precise, the
d is for the cut action and
G is for moving the cursor to the end of the file. This way,
dG cuts all the text from the current cursor position to the end of the file.
This is why you need to be at the beginning of the file if you want to delete all the lines in Vim.
If you are familiar with Vim modes, you can switch to the visual mode and use the arrow keys to select all the text and then delete it. However, this option is also not very convenient.
There are other ways to deal with multiple line deletion in Vim but the focus is on deleting all the lines.
If you are interested in learning more than just the Vim basics, I highly recommend using this program by Jovica Ilic.
Questions or suggestions? Please leave a comment.