384

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.

Cold
  • 4,153

12 Answers12

646

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.

Anthon
  • 79,293
  • 23
    Pro tip. No more ESC+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd... – Matt Borja Nov 03 '16 at 19:38
  • 5
    @rdev5 try . next time to repeat a command – Gazihan Alankus Mar 12 '19 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 – theprogrammer May 02 '20 at 10:04
248

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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Archemar
  • 31,554
66

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)
Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • I want delete all in one time, If i have 10000000000000 lines, using dG i will need use dG how many times? – Cold Oct 13 '14 at 11:41
  • 30
    @Cold: ...once? – Chris Down Oct 13 '14 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 '14 at 11:44
  • once @ChrisDown. – Cold Oct 13 '14 at 11:45
  • 7
    gg is vim specific. It's 1G in traditional vi. – Stéphane Chazelas Oct 13 '14 at 12:53
  • @StéphaneChazelas: Thanks for the note, I've updated the answer. – Chris Down Oct 13 '14 at 12:59
  • 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 +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 '14 at 10:43
  • 2
    @Caleb: Like I said, in any POSIX-compliant shell. :-) Neither of those two situations are POSIX-complaint. – Chris Down Oct 16 '14 at 15:38
35

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?

kenorb
  • 20,988
  • 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 '14 at 17:12
  • 2
    Upvoted. dG is what I always use. –  Oct 13 '14 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 '14 at 02: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 '19 at 18:11
35

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.

TankorSmash
  • 850
  • 6
  • 8
13

Go to the beginning of the file and press dG.

HalosGhost
  • 4,790
cppcoder
  • 239
7

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.

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

Another solution:

truncate -s 0 file
phk
  • 5,953
  • 7
  • 42
  • 71
slawx
  • 41
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

mc0e
  • 1,086
  • 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("%"))) – Stéphane Chazelas Feb 07 '19 at 11:51
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

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.

Atul
  • 1,891
-1
> file.txt

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

Romeo Ninov
  • 17,484
shadow
  • 1
  • 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 Feb 07 '19 at 11:39
  • 2
    Besides, the point of the question is ... "using vi" – Jeff Schaller Feb 07 '19 at 11:40