How to Delete all Text in a File Using Vi/Vim Editor?

Vi and Vim are powerful text editors widely used in Unix/Linux environments for editing plain text files, system configuration files, and programming source code. Vi stands for visual editor, while Vim stands for Vi Improved.

Originally developed by Bill Joy in 1976, these editors offer extensive functionality including syntax highlighting, search-and-replace, macros, and efficient handling of large files. One common task is completely clearing a file's content, which can be accomplished using specific commands.

Basic Vi/Vim Commands

Opening a File

To open a file in Vi/Vim, use the command line to navigate to the file's directory and type vi filename. Alternatively, use the full path:

vi /home/user/documents/file.txt

Editor Modes

Vi/Vim operates in different modes:

  • Command Mode Default mode for navigation and commands (press Esc)

  • Insert Mode For typing text (press i to enter)

  • Visual Mode For selecting text blocks (press v)

Basic Deletion Commands

Command Action
x Delete single character at cursor
dd Delete entire current line
D Delete from cursor to end of line
5dd Delete 5 lines starting from current line

How to Delete All Text in a File

Method 1: Using :%d Command

The most efficient way to delete all content in a file:

  1. Open the file:

    vi filename.txt
    
  2. Enter Command Mode: Press Esc key

  3. Delete all text: Type the following command and press Enter:

    :%d
    
  4. Save and exit:

    :wq
    

Method 2: Using Visual Mode

Alternative approach using visual selection:

  1. Press Esc to enter Command Mode

  2. Go to the beginning of file: gg

  3. Enter Visual Mode: V (capital V for line mode)

  4. Select all content: G (go to end of file)

  5. Delete selected text: d

Method 3: Using Range Commands

Delete all lines using line range:

:1,$d

This command deletes from line 1 to the last line ($).

Command Breakdown

Command Explanation
:%d % = entire file range, d = delete
:1,$d Delete from line 1 to last line
ggVGd gg = top, V = visual line, G = bottom, d = delete

Saving and Exiting

After deleting all text, choose the appropriate exit command:

  • :wq Save changes and quit

  • :x Save (if modified) and exit

  • :q! Quit without saving changes

  • :wq! Force save and quit

Important Notes

  • No undo after save: Once you save and exit, the deletion is permanent

  • Backup recommended: Create a backup before clearing important files

  • Alternative approach: Use :e! to reload the original file if you haven't saved yet

Conclusion

Deleting all text in a file using Vi/Vim can be accomplished efficiently with the :%d command. This method instantly clears all content without confirmation, making it essential to use with caution. Always ensure you have backups of important files before performing bulk deletions, as the changes become permanent once saved.

Updated on: 2026-03-17T09:01:38+05:30

45K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements