361

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
  • 8
    I think dG should work.
    – MadTux
    Oct 13, 2014 at 12:57
  • 12
    @MadTux, only if you start on the first line. 1GdG would work from anywhere.
    – Holloway
    Oct 13, 2014 at 14:02
  • 1
    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
    Oct 13, 2014 at 16:40
  • You are right @tripleee
    – Cold
    Oct 13, 2014 at 17:05
  • 7
    Note that not using vi, >test.txt is sufficient to truncate it to zero length - no echo needed.
    – abligh
    Oct 13, 2014 at 21:47

14 Answers 14

598

In vi do

:1,$d

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.

3
  • 17
    Pro tip. No more ESC+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd...
    – Matt Borja
    Nov 3, 2016 at 19:38
  • 4
    @rdev5 try . next time to repeat a command Mar 12, 2019 at 22:30
  • 1
    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 May 2, 2020 at 10:04
235

In vi I use

:%d

where

  • : 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

where

  • /dev/null is a special 0 byte file
  • if is the input file
  • of is the ouput file
8
  • I was thinking that using dd i need pess for every line on file.
    – Cold
    Oct 13, 2014 at 11:43
  • Good. It's more simple than i expected...
    – Cold
    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
    Oct 13, 2014 at 13:03
  • 1
    @depquid Oups! now I come to think of it...
    – Archemar
    Oct 13, 2014 at 13:11
  • 2
    Or : >test.txt which is only very marginally longer.
    – tripleee
    Oct 13, 2014 at 16:39
64

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)
8
  • 30
    @Cold: ...once?
    – Chris Down
    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
    Oct 13, 2014 at 11:44
  • 6
    gg is vim specific. It's 1G in traditional vi. Oct 13, 2014 at 12:53
  • 1
    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
    Oct 16, 2014 at 10:43
  • 1
    @Caleb: Like I said, in any POSIX-compliant shell. :-) Neither of those two situations are POSIX-complaint.
    – Chris Down
    Oct 16, 2014 at 15:38
36

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?

4
  • 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.
    Oct 13, 2014 at 17:12
  • 2
    Upvoted. dG is what I always use. 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
    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
    Oct 16, 2019 at 18:11
33

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.

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

Go to the beginning of the file and press dG.

6

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.

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

Another solution:

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

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
  • 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("%"))) Feb 7, 2019 at 11:51
1

Type : and %d

(Not in insert mode. If so, press escape first)

1
0

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.

-1

You can use a trick to delete a big number of lines. Make sure you're not in insert mode (press ESC) then type 1000dd.

2
  • 1
    You should include the explanation on what the command does/how it works (deletes next 1000 lines, which doesn't delete all lines if cursor is not at the first line).
    – sebasth
    Oct 16, 2018 at 7:45
  • this is why I called it a trick :)
    – Radu
    Oct 16, 2018 at 7:49
-1

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.

-1
> file.txt

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

5
  • 1
    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
    Feb 7, 2019 at 11:39
  • Besides, the point of the question is ... "using vi"
    – Jeff Schaller
    Feb 7, 2019 at 11:40
  • I tried exactly what you suggested. I got bash: > file.txt: command not found.
    – roaima
    Feb 7, 2019 at 11:42
  • @roaima You'd get that if you quote the thing as "> file.txt".
    – Kusalananda
    Mar 10, 2019 at 16:22
  • 1
    @JeffSchaller : vi >file.txt ;-)
    – Kusalananda
    Mar 10, 2019 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.