Clearing a file in place is a common Linux task—whether you’re trimming a log, resetting a config, or preparing a clean dataset for tests. Doing it inside the editor is often safer than deleting and recreating the file, because it preserves permissions, ownership, and any links or active file handles.
This guide shows:
- Multiple ways to wipe a file in vi/vim
- Why clearing is safer than deleting and recreating
- How to save, quit, or bail out without saving afterward
When should you clear (not delete) a file?
- Large/active log files: Services may still have the file open. Deleting it can cause errors or simply leave disk space allocated until the process restarts. Clearing the file in place avoids both issues.
- Temporary/cache files: Reset a file’s contents between runs without changing its permissions or path.
- Configuration resets: Sometimes it’s cleaner to empty a config and paste a known-good template than to juggle new files, links, and ACLs.
- Development/testing: Ensure one test run doesn’t bleed into the next by starting with an empty input/output file.
Why not just delete and recreate?
Deleting a file resets ownership and permissions, can break symlinks/hard links, and may disrupt processes still holding the file open. Clearing in place avoids all of that.
Optional: make a quick practice file
tr -dc '[:alnum:]\n' < /dev/urandom | head -c 1024 > /tmp/tmp.abc
vim /tmp/tmp.abc
Code language: JavaScript (javascript)
Method 1 — ggdG (Normal mode)
This sequence combines three motions/operators:
gg
→ jump to the first lined
→ delete operator (waits for a motion)G
→ jump to the last line
Together, ggdG
means “delete from the first line to the last line”.
Steps
- Open the file:
vim your_file.txt
- Press Esc to ensure Normal mode
- Type ggdG (no Enter needed) → the buffer is now empty
- Press i to enter Insert mode if you want to add new content
- Save/exit as needed (see below)
Method 2 — :%d (Command-line/Ex mode)
:%d
deletes the whole buffer using Ex commands:
:
enters Command-line mode%
targets the entire filed
is delete
Steps
vim your_file.txt
- Press Esc to ensure Normal mode
- Type
:%d
then Enter → file is cleared - Press i to insert new content (optional)
- Save/exit as needed
Tip: On very large files,
:%d
can be marginally faster because it’s a single Ex command.
Saving and exiting (pick one)
- Save without quitting:
- Esc, then
:w
Enter
- Esc, then
- Save and quit:
- Esc, then
:wq
Enter - or press ZZ (uppercase) in Normal mode
- Esc, then
- Quit without saving (discard changes):
- Esc, then
:q!
Enter
- Esc, then
Quick reference
Command | What it does | Notes |
---|---|---|
ggdG | Delete all text (Normal mode) | No Enter required |
:%d | Delete all text (Ex mode) | Type :%d then Enter |
dd | Delete current line | Repeat with a count: 5dd |
u | Undo last change | Press multiple times to step back |
:q! | Quit without saving | Discards buffer changes |
:wq / ZZ | Save and quit | ZZ works only in Normal mode |
FAQs
How do I delete a single line instead of everything?
Use dd
on the target line. To delete multiple lines, prefix with a count (e.g., 10dd
).
Can I undo deleting everything?
Yes. Press u (undo). If you used ggdG
or :%d
, one u
restores the buffer.
What’s the difference between ggdG
and :%d
?
Both clear the file. ggdG
is a Normal-mode motion+operator; :%d
is a single Ex command. On huge files, :%d
is often a touch faster.
How do I clear a file but keep it unchanged on disk?
If you cleared by mistake, don’t save. Exit with :q!
to discard changes.
Is there a non-vim way to truncate quickly?
Yes. From the shell, you can do:
: > file # POSIX shell built-in (no output, redirected to file)
truncate -s 0 file
> file # bash/ksh redirection
Code language: PHP (php)
These keep ownership/permissions intact and zero the file instantly.
Final notes
Once you know them, ggdG
and :%d
become muscle memory. They’re safe, quick, and preserve file metadata—perfect for logs, configs, and test artifacts. If you’re working in automation, shell-level truncation (: > file
or truncate -s 0 file
) is also reliable and script-friendly.