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?

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

  • 8
    I think dG should work.
    – MadTux
    Commented Oct 13, 2014 at 12:57
  • 12
    @MadTux, only if you start on the first line. 1GdG would work from anywhere.
    – Holloway
    Commented Oct 13, 2014 at 14:02
  • 2
    echo | test.txt is not a valid command, unless test.txt is an executable script. I'm guessing you mean echo >test.txt instead?
    – tripleee
    Commented Oct 13, 2014 at 16:40
  • 1
    You are right @tripleee
    – Cold
    Commented Oct 13, 2014 at 17:05
  • 8
    Note that not using vi, >test.txt is sufficient to truncate it to zero length - no echo needed.
    – abligh
    Commented Oct 13, 2014 at 21:47

12 Answers 12


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.

  • 23
    Pro tip. No more ESC+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd...
    – Matt Borja
    Commented Nov 3, 2016 at 19:38
  • 5
    @rdev5 try . next time to repeat a command Commented Mar 12, 2019 at 22:30
  • 2
    Even with :%d and enter, its way too many strokes. I am trying to get into vim but without vim ctrl + a and del button is much more simpler than what vim provides for this. Just two strokes(ctrl+a, del) unlike :%d which has 4 strokes including the enter command Commented May 2, 2020 at 10:04

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
  • I was thinking that using dd i need pess for every line on file.
    – Cold
    Commented Oct 13, 2014 at 11:43
  • Good. It's more simple than i expected...
    – Cold
    Commented Oct 13, 2014 at 12:50
  • 12
    You need to distinguish between dd the vi command (which the OP meant) and dd the utility, which you give an example of. Also, > test.txt may not work as expected in non-bash shells (e.g. zsh).
    – depquid
    Commented Oct 13, 2014 at 13:03
  • 1
    @depquid Oups! now I come to think of it...
    – Archemar
    Commented Oct 13, 2014 at 13:11
  • 2
    Or : >test.txt which is only very marginally longer.
    – tripleee
    Commented Oct 13, 2014 at 16:39

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)
  • 30
    @Cold: ...once?
    – Chris Down
    Commented Oct 13, 2014 at 11:43
  • 5
    G represents last line. If you are on the first line (gg), dG tells vi to remove all the lines from current line (first line) to the last line. So, you do it in one shot.
    – unxnut
    Commented Oct 13, 2014 at 11:44
  • 7
    gg is vim specific. It's 1G in traditional vi. Commented Oct 13, 2014 at 12:53
  • 2
    Your shell syntax is dependent on configuration for some shells. Both bash and zsh (zsh by default) interactive shells can wait for input on STDIN after receiving that command and an additional <CTRL>+D is necessary to actually clear the file. The ability to run that is also dependent on CLOBBER settings. >| test.txt < /dev/null is somewhat more robust as it will always clobber the file and avoid waiting for input on interactive terminals.
    – Caleb
    Commented Oct 16, 2014 at 10:43
  • 2
    @Caleb: Like I said, in any POSIX-compliant shell. :-) Neither of those two situations are POSIX-complaint.
    – Chris Down
    Commented Oct 16, 2014 at 15:38

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.

  • rsrs. I'm lazy too and use :$d or :1,$d is the faster way (I write less characters) kkk
    – Cold
    Commented Oct 15, 2014 at 15:52
  • 3
    :%d is good alternative, since you only use 5 keystrokes that way too. Commented Oct 15, 2014 at 15:56
  • 2
    @TankorSmash gg is a vim command, not vi.
    – remmy
    Commented Oct 15, 2014 at 16:56
  • good catch, my mistake Commented Oct 15, 2014 at 17:27

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 foo.bar.

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

  • 3
    +1 for being more ergonomic than :1,$d. Not that our fingers aren't wired for typing colon all the time now, anyway ;)
    – Aaron R.
    Commented Oct 13, 2014 at 17:12
  • 2
    Upvoted. dG is what I always use.
    – user6517
    Commented Oct 13, 2014 at 23:35
  • 1
    I find this (ggdG) the easiest method in vim. The reason this answer isn't upvoted as others is that gg is non-existent in pure vi?
    – thameera
    Commented Oct 15, 2014 at 2:15
  • There are of course other similar variations if you're not already at the top of the file, such as 1GdG or Gd1G. But if you're using vim, then ggdG is the easiest to type.
    – jrw32982
    Commented Oct 16, 2019 at 18:11

Go to the beginning of the file and press dG.


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.

  • If you use d vertically, it automatically applies linewise. dl deletes a character to the right, dj deletes a line down, for example. Commented Oct 15, 2014 at 15:49

Another solution:

truncate -s 0 file
  • Not really simpler than >file but OK.
    – phk
    Commented Jul 15, 2016 at 14:26
  • 1
    Seem to be a good method, but the target is doing it using vi. Tks
    – Cold
    Commented Jul 18, 2016 at 8:35

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

  • 1
    :!:>% is also dangerous as vi doesn't escape the file name for the shell (don't do it when editing a file called $(reboot) for instance). You'd need something :call system(":>" . shellescape(expand("%"))) Commented Feb 7, 2019 at 11:51

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.


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.

> file.txt

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

  • 2
    Hello and welcome to the U&L stack exchange site. Please review the help center for information on how to best post this site. We want to cultivate informative and unique questions and answers on this site. Your answer is identical to other answers and comments on this post. Please edit your post to be a more unique and informative answer please. Thank you!
    – kemotep
    Commented Feb 7, 2019 at 11:39
  • 2
    Besides, the point of the question is ... "using vi"
    – Jeff Schaller
    Commented Feb 7, 2019 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .