Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Ubuntu and trying to delete all 100 lines from vi editor but I got interview question of doing this in one command.

share|improve this question
Not a VI solution, but from the command line: echo -n > <file> removes all lines from a file (actually just overwrites the file with ""). – Der Flatulator Aug 26 '13 at 9:41
@Der No need to use echo at all for that; > $file works just fine. – Michael Kjörling Aug 26 '13 at 13:01
What kind of company is that, asking questions about vi in an interview? – Michael Kjörling Aug 26 '13 at 13:02
This question is easily answered with a rudimentary knowledge of vi. On a computer with vim installed (on Ubuntu I'd recommend the vim-gtk package, since that gives you access to the xclipboard within vim), type vimtutor at the command line (not within vim) to get an interactive tutorial of the basics. It shouldn't take more than half an hour, and it's the best starter's guide to using vi/vim that you're likely to find. – evilsoup Aug 26 '13 at 14:03
@MichaelKjörling I could imagine it coming up in an interview if the applicant had either mentioned prior experience with vi (verbally or by resumé) or if the question were prefaced with, "Okay, as a sysadmin you must know something of command line editors, right? You've used vi?" – Wildcard May 13 at 19:03

In normal mode, do 100dd. dd deletes the current line. Prefacing that command with 100 causes it to repeat 100 times.

share|improve this answer
wrong anwser it is only deleting current line – mayur Mehta Aug 27 '13 at 8:48
I want to delete 100 lines using one command at a one time – mayur Mehta Aug 27 '13 at 8:49
100dd is the correct answer. I wonder what you're doing. – ott-- Aug 27 '13 at 22:54
@mayurMehta don't know which company is asking questions like this, and don't know what they are expecting as the answer. But any novice VI user will know that 100dd is the correct answer. – Munim Aug 30 '13 at 4:51
@vgoff I don't know.. If they are not looking for a general solution to test your basic vim skills, then it is just a trick question to stump interviewees. I'd say 100dd is the answer. If the interviewer asks "What happens if the file has less than 100 lines from the current cursor?", I'd answer: "The command deletes what it can from the current cursor." That is perfectly reasonable. Unless the interviewer clarifies, why would the cursor not be on the first line? I hate trick questions like these. – Munim Sep 13 '13 at 7:02
  1. delete 100 lines forward from (including) the current one

    • repeat dd (delete current line) 100 times:

    • delete from current line to 99 lines forward

  2. delete 100 lines backwards from (including) the current one

  3. delete lines in a specific range by line number

  4. delete lines in a range beginning with the current line


etc. etc.

share|improve this answer
number 4 will delete current line (0) until 101th (0+100) lines, hence 101 lines instead of the asked 100. try :.,+99d – Olivier Dulac Aug 26 '13 at 12:40
Drat, and I got the movement ones correct! Thanks. – Useless Aug 26 '13 at 13:34
100dd didn't work for me either - but #4 above works fine for me in VI under Solaris 10. – DemiSheep Jul 1 '14 at 15:00

If all lines in the file are to be deleted, this vi command specifies the range of deletion:


1 denotes the first line and $ denotes the last line

share|improve this answer
or :%d (in ":" context, ":%..." is shortcut for ":1,$...") – Olivier Dulac Aug 26 '13 at 12:35
also nice: d G keys &rarr; kill all content from actual [e.g. top line] to bottom line – erch Aug 26 '13 at 12:39

No matter where are you are in the file, you can do ggdG

:%d would do better in less key presses. Well, counting the shift and enter keys, actually two more key presses than the above.

Doing something like 100dd would only delete up to 100 lines from where you are, and that may not delete all lines from the file, depending on your current line position.

share|improve this answer

You want to delete all the lines in a file? Open the file with vi. While at the beginning of the first line -- the default location of the cursor when you just open the file -- press Esc followed by dG.

Just to note, the action d indicates delete, and G indicates the last line of the file. So, while at any line you press dG, it deletes all the line starting from the current line till the last one.

If you know which consecutive lines to delete -- say, from line 101 to 200 -- type in the following key sequences: Esc:101,200d.

share|improve this answer
No need to press escape if you have just opened the file and are at the beginning of the file. You are automatically (by default) in normal mode. – vgoff Jul 21 '15 at 6:54

If u want to delete all lines in a file you can use this command:

> filename

Suppose you have a file named example.txt which contains some data and you want remove all lines from this file. To do this you can issue this command:

> example.txt

After typing this you will get an empty file.

share|improve this answer
The question is about using vi to delete the lines. – stoeff Apr 2 '15 at 15:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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