Delete All Text in a File Using vi Editor
In this article
To delete all text in a file using the vi editor, open the file and type gg to move the cursor to the first line, then type dG to delete everything from the cursor to the end of the file. This command removes all lines, leaving an empty file; you can then save and exit with :wq.
Why You Might Need to Clear a File in vi
vi (and its improved version vim) is a powerful, modal text editor preinstalled on most Unix-like systems, including Linux and macOS. Users often need to quickly wipe a file's contents—perhaps to reset a configuration file, clear a log, or start a script from scratch. While you could delete the file and recreate it, using vi keeps the file's permissions, ownership, and inode intact, which is useful for system files or when you want to avoid changing metadata.
Understanding vi Modes
vi operates in two primary modes:
- Command mode (default when you open a file): keystrokes are interpreted as commands, not text insertion.
- Insert mode (entered with
i,a, oro): keystrokes are inserted as text.
To delete all text, you must be in command mode. If you're unsure, press Esc to return to command mode. The commands gg and dG are executed in command mode.
Step-by-Step: Delete All Text in vi
- Open the file in vi:
vi filename.txt(orvim filename.txt). - Press
Escto ensure you're in command mode. - Type
ggto move the cursor to the first line of the file. - Type
dG(ordggif you're at the end and want to delete upward). This deletes from the current line to the last line (G), effectively removing all lines. - Verify the file is empty by typing
:%!cat(or simply:1,$das an alternative). - Save and exit with
:wq(write and quit). If you want to exit without saving, use:q!.
Alternative command: :%d deletes all lines in the file (the % represents the whole file). This is often simpler and works from any cursor position.
Common Mistakes and How to Avoid Them
- Being in insert mode: If you type
ggwhile in insert mode, it will insert the literal characters "gg". Always pressEscfirst. - Using
ddrepeatedly:dddeletes only the current line. To delete all lines, you'd need to repeat it many times—usedGor:%dinstead. - Forgetting to save: After deleting, if you exit with
:q(withoutw), the file remains unchanged. Use:wqto save the deletion. - Deleting in the wrong direction:
dGdeletes from cursor to end. If your cursor is not at the top, useggfirst. Alternatively,dggdeletes from cursor to beginning.
Alternative Methods to Clear a File
While vi is handy, there are other ways to empty a file:
- Using shell redirection:
> filenametruncates the file to zero length (requires shell permission). - Using
truncatecommand:truncate -s 0 filenameis a dedicated tool for this. - Using
cat /dev/null:cat /dev/null > filenameachieves the same result. - Using
echo -n:echo -n > filenamealso works.
These shell methods are faster for large files, but vi is useful when you're already editing and want to start fresh without leaving the editor.
Safety and Precautions
Deleting all text is irreversible in vi unless you have unsaved changes. Once you save with :wq, the previous content is gone. To avoid accidental data loss:
- Make a backup before clearing:
cp filename filename.bak. - If you're not sure, use
:w filename.newto save a copy before wiping the original. - Be careful with system files—clearing a configuration file may break services. Always verify the file's purpose.
Frequently Asked Questions
How do I delete all text in vim and start typing?
After deleting all text with gg then dG, press i to enter insert mode and start typing. The file will be empty, so your new text will be the only content.
What is the difference between dG and :%d?
dG deletes from the current cursor line to the end of the file. :%d deletes all lines regardless of cursor position. Both achieve the same result if you first move to the top with gg, but :%d is more direct.
Can I undo a delete-all in vi?
Yes, if you haven't saved yet. Press u in command mode to undo the last change. If you've already saved with :wq, the undo history is lost, and you cannot recover the text unless you have a backup.
Does deleting all text remove the file itself?
No, it only empties the file's content. The file remains on disk with its permissions and name. To delete the file entirely, use rm filename in the shell.
Why does my vi show ~ lines after deletion?
The tilde (~) characters indicate lines that are not part of the file—they're just placeholders in the editor window. After deleting all text, the file is empty, and vi displays ~ for the empty buffer area. This is normal.