Overview
The below shortcut can be used to delete all contents of a file using bash/terminal when using vi or vim.
dG
Example
Let’s see an example
- First let’s create and write to a temp file
echo $'This is \ntest line' > temp.txt
- Let’s print the content of the file to verify
~ $ cat temp.txt
This is
test line
- Run vi or vim command now
~ $ vi temp.text
In the vi editor, you will see below
This is
test line
~
~
"temp.txt" 2L, 19C
Now press ‘d’ and then ‘G’ ( Use the shift key to make ‘g’ capital). Note that these keys have to be pressed one after the other and not together. Once you press the key the contents will be clear. In the vi or vim editor, you will see this
~
~
--No lines in buffer--
Now save the file. Use shortcut
ESC key + :wg
Now check the contents of the file again. It will be empty
~ $ cat temp.txt