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.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

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.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

5 Responses

  1. Sergio Araujo says:

    1,$ is equal to %, hence :%d does do job with less keystrokes.

  2. jiaowen says:

    In insert mode, you can type %d to clear the text.

  3. dragonmouth says:

    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.

Leave a Reply to Aaron Kili Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.