How can I delete all lines in a file using vi?

How can I delete all lines in a file using vi?

At moment I do that using something like this to remove all lines in a file:

echo > test.txt

How can I delete all lines using vi?

Using dd is not a good option. There can be many lines.

Asked By: Cold


I’d recommend that you just do this (should work in any POSIX-compliant shell):

> test.txt

If you really want to do it with vi, you can do:

  • 1G (go to first line)
  • dG (delete to last line)
Answered By: Chris Down

In vi do


to delete all lines.

The : introduces a command (and moves the cursor to the bottom).
The 1,$ is an indication of which lines the following command (d) should work on. In this case the range from line one to the last line (indicated by $, so you don’t need to know the number of lines in the document).
The final d stands for delete the indicated lines.

There is a shorter form (:%d) but I find myself never using it. The :1,$d can be more easily “adapted” to e.g. :4,$-2d leaving only the first 3 and last 2 lines, deleting the rest.

Answered By: Anthon

In vi I use



  • : tells vi to go in command mode
  • % means all the lines
  • d : delete

On the command line,

> test.txt

will do also.

What is the problem with dd?

dd if=/dev/null of=test.txt


  • /dev/null is a special 0 byte file
  • if is the input file
  • of is the ouput file
Answered By: Archemar

If your cursor is on the first line (if not, type: gg or 1G), then you can just use dG. It will delete all lines from the current line to the end of file. So to make sure that you’ll delete all the lines from the file, you may mix both together, which would be: ggdG (while in command mode).

Or %d in Ex mode, command-line example: vim +%d

Related: How I can delete in VIM all text from current line to end of file?

Answered By: kenorb

I always use ggVG

  • gg jumps to the start of the current editing file
  • V (capitalized v) will select the current line. In this case the first line of the current editing file
  • G (capitalized g) will jump to the end of the file. In this case, since I selected the first line, G will select the whole text in this file.

Then you can simply press d or x to delete all the lines.

Answered By: yegle

Go to the beginning of the file and press dG.

Answered By: cppcoder

I’m a lazy dude, and I like to keep it simple. ggdG is five keystrokes including Shift

gg goes to the first line in the file, d is the start of the delete verb and G is the movement to go to the bottom of the file. Verbosely, it’s go to the beginning of the file and delete everything until the end of the tile.

Answered By: TankorSmash

note that in your question, echo > test.txt creates a file with a single line break in it, not an empty file.

From the shell, consider using echo -n > test.txt or : > test.txt.

While I’d generally use a vi editing command (I use ggdG), you can also call out to the shell with a reference to the current file like so:


It’s nearly as concise as ggdG, but harder to type, and you also have to confirm that you want to reload the modified file, so I don’t particularly recommend it in this case, but knowing how to use shell commands from vi like this is useful.

breaking it down:

  • : initiate a vi command
  • ! initate a shell command
  • : this is a shell builtin command with empty output
  • > redirect the output
  • % vi substitutes this with the name of the current file

The suggested :1,$d is also a good one of course, and just while I’m at it there’s also 1GdG

Answered By: mc0e

Another solution:

truncate -s 0 file
Answered By: slawx

I type gg to reach the top of the lines and then do a 100 dd. This brings the pointer to the top line of editor and clears 100 lines from there. You may have to adjust the number to add more lines if the file is lengthier.

Answered By: Atul
> file.txt

You don’t even have to open vi unless you really want to.

Answered By: shadow

I do a dgg, which deletes all lines from cursor (which is typically EOF when you open a file) to the top, which is the quickest.

Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.