In Vim, the contents of a file can be emptied with the following command:
-
vim filename
Open the file to be emptied: Enter open file in the terminal , wherefilename
is the name of the file you want to edit. -
Esc
Enter command mode: Make sure you are in Vim's command mode by pressing the key on your keyboard . (that is, a colon in the lower left corner) -
Empty file content: In command mode, enter the following command:
:1,$d
This command means to delete the content from the first line to the last line. :
It means to enter the command mode, 1,$
means to select the range from the first line to the last line, and d
means to delete.
- Save and exit: Press the key on the keyboard
Esc
to enter command mode, then enter to:wq
save and exit the file.
In this way, you have successfully cleared the contents of the file. Please note that the file cannot be recovered after being emptied, please proceed with caution.