How to delete all lines of file in Vim

Problem

To delete all the lines of a file that is open in Vim.

Solution

  1. Type gg to move the cursor to the first line of the file, if it is not already there.
  2. Type dG to delete all the lines.

Deleting all the lines of a file is not a frequently used operation. So, I need a command that is easy to recall, even though it is not used much. I find dG easy to remember since I frequently use dd to delete a line, and also gg and G, which move the cursor to the first and last line of the file respectively.

Tried with: Vim 7.3

Advertisements

18 thoughts on “How to delete all lines of file in Vim”

    1. Antrix: That was a very comprehensive reply for that SO question! πŸ™‚

      Since most of my work happens in Visual Studio, Vim is relegated to be the text editor for “everything else”. Hence I have tended to stick with the vi/vim commands that I am familiar with and have not really probed much into its ex commands, which I realize I need to.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s