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

Vim is a great tool for editing text or configuration files in Linux. One of the lesser-known Vim tricks is clearing or deleting all text or lines in a file. Although, this is not a frequently used operation, its a good practice to know or learn it.

In this article, we will describe steps on how to delete, remove or clear all text in a file using a Vim editor in different vim modes.

Read Also: 5 Ways to Empty or Delete a Large File Content in Linux

The first option is to remove, clear or delete the all lines in a file in the normal mode (note that Vim starts in “normal” mode by default). Immediately after opening a file, type “gg” to move the cursor to the first line of the file, assuming it is not already there. Then type dG to delete all the lines or text in it.

Delete Complete Text in Vi Editor
Delete Complete Text in Vi Editor

If Vim is in another mode, for example, insert mode, you can access normal mode by pressing Esc or <C-[>.

Alternatively, you can also clear all lines or text in Vi/Vim in command mode by running the following command.

:1,$d 
Delete Text in Vi Editor
Delete Text in Vi Editor

Last but not least, here is a list of Vim articles that you will find useful:

  1. 10 Reasons Why You Should Use Vi/Vim Text Editor in Linux
  2. Learn Useful ‘Vi/Vim’ Editor Tips and Tricks to Enhance Your Skills
  3. How to Enable Syntax Highlighting in Vi/Vim Editor
  4. How to Password Protect a Vim File in Linux
  5. 6 Best Vi/Vim-Inspired Code Editors for Linux
  6. PacVim – A Game That Teaches You Vim Commands

In this article, we have explained how to clear or delete all lines or text in a file using Vi/Vim editor. Remember to share your thoughts with us or ask questions using the comment form below.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Aaron Kili
Aaron Kili
Contributor
About the author

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

5 Comments

Leave a Reply
  1. Wouldn’t it be simpler to just delete and then recreate the file using the file manager?

    I know that CLI is generally faster and more efficient than GUI but in this case you are just unnecessarily complicating things by using CLI.

    Reply
    • @Dragonmouth

      This article is intended to show users that Vim supports the explained option, especially after opening a file. Thanks for the feedback.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.