Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Delete All Text in a File Using vi Editor

· 4 min read · Home Tech

In this article
  1. Why You Might Need to Clear a File in vi
  2. Understanding vi Modes
  3. Step-by-Step: Delete All Text in vi
  4. Common Mistakes and How to Avoid Them
  5. Alternative Methods to Clear a File
  6. Safety and Precautions
  7. Frequently Asked Questions

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, or o): 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

  1. Open the file in vi: vi filename.txt (or vim filename.txt).
  2. Press Esc to ensure you're in command mode.
  3. Type gg to move the cursor to the first line of the file.
  4. Type dG (or dgg if 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.
  5. Verify the file is empty by typing :%!cat (or simply :1,$d as an alternative).
  6. 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 gg while in insert mode, it will insert the literal characters "gg". Always press Esc first.
  • Using dd repeatedly: dd deletes only the current line. To delete all lines, you'd need to repeat it many times—use dG or :%d instead.
  • Forgetting to save: After deleting, if you exit with :q (without w), the file remains unchanged. Use :wq to save the deletion.
  • Deleting in the wrong direction: dG deletes from cursor to end. If your cursor is not at the top, use gg first. Alternatively, dgg deletes 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: > filename truncates the file to zero length (requires shell permission).
  • Using truncate command: truncate -s 0 filename is a dedicated tool for this.
  • Using cat /dev/null: cat /dev/null > filename achieves the same result.
  • Using echo -n: echo -n > filename also 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.new to 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.