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.

  • Not a VI solution, but from the command line: echo -n > <file> removes all lines from a file (actually just overwrites the file with "").
    – azz
    Aug 26, 2013 at 9:41
  • 3
    @Der No need to use echo at all for that; > $file works just fine.
    – user
    Aug 26, 2013 at 13:01
  • 4
    What kind of company is that, asking questions about vi in an interview?
    – user
    Aug 26, 2013 at 13:02
  • 2
    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, 2013 at 14:03
  • 1
    @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, 2016 at 19:03

6 Answers 6


In normal mode, do


dd deletes the current line. Prefacing that command with 100 causes it to repeat 100 times.

If there are fewer than 100 lines in the file starting from the current line, depending on the vi implementation, it will either fail to delete any or delete as many as there are. In the case of vim, that depends on whether the cp aka compatible option is on or not.

  • wrong anwser it is only deleting current line Aug 27, 2013 at 8:48
  • I want to delete 100 lines using one command at a one time Aug 27, 2013 at 8:49
  • 15
    100dd is the correct answer. I wonder what you're doing.
    – ott--
    Aug 27, 2013 at 22:54
  • 1
    @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, 2013 at 4:51
  • 1
    @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, 2013 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.

  • 3
    number 4 will delete current line (0) until 101th (0+100) lines, hence 101 lines instead of the asked 100. try :.,+99d Aug 26, 2013 at 12:40
  • 100dd didn't work for me either - but #4 above works fine for me in VI under Solaris 10.
    – DemiSheep
    Jul 1, 2014 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

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

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.

  • 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, 2015 at 6:54

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.


Esc n dd. n is the number of lines to delete from and including the current line. Press "Esc" button, then "n" and then "dd". To better your understanding

  • What is the need of escape here
    – Prvt_Yadav
    May 7, 2018 at 6:28
  • This answer is identical to the accepted answer.
    – Kusalananda
    May 7, 2018 at 6:43
  • Escape is just in case you are in insert mode.
    – Rida Abid
    May 7, 2018 at 8:48

You must log in to answer this question.

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