Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
2  
@Der No need to use echo at all for that; > $file works just fine. –  Michael Kjörling Aug 26 '13 at 13:01
3  
What kind of company is that, asking questions about vi in an interview? –  Michael Kjörling Aug 26 '13 at 13:02
1  
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
add comment

5 Answers

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
9  
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
    
Making the assumption that the file is exactly 100 lines long, and the cursor is on line 1, this is the correct answer. If the cursor is on line 2, then no action will happen if you press 100dd. So a novice vim user, as @Munim indicates may indeed think that this is the correct answer, but they would be correct only in a very specific case. And this would not work from the command line. Unless of course, you record the command and read it in later vim -s 100dd_command file.txt –  vgoff Sep 12 '13 at 23:06
show 4 more comments
  1. delete 100 lines forward from (including) the current one

    • repeat dd (delete current line) 100 times:

      100dd
      
    • delete from current line to 99 lines forward

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

    d99k
    
  3. delete lines in a specific range by line number

    :1,100d
    
  4. delete lines in a range beginning with the current line

    :.,.+99d
    

etc. etc.

share|improve this answer
2  
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
add comment

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

:1,$d

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 –  Chirp. Not Luke. Aug 26 '13 at 12:39
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.