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.
6 Answers
In normal mode, do
100dd
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 linemayur Mehta– mayur Mehta2013-08-27 08:48:15 +00:00Commented Aug 27, 2013 at 8:48
-
I want to delete 100 lines using one command at a one timemayur Mehta– mayur Mehta2013-08-27 08:49:29 +00:00Commented Aug 27, 2013 at 8:49
-
15
100ddis the correct answer. I wonder what you're doing.ott--– ott--2013-08-27 22:54:31 +00:00Commented 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– Munim2013-08-30 04:51:36 +00:00Commented 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– Munim2013-09-13 07:02:08 +00:00Commented Sep 13, 2013 at 7:02
delete 100 lines forward from (including) the current one
repeat dd (delete current line) 100 times:
100dddelete from current line to 99 lines forward
d99j
delete 100 lines backwards from (including) the current one
d99kdelete lines in a specific range by line number
:1,100ddelete lines in a range beginning with the current line
:.,.+99d
etc. etc.
-
3number 4 will delete current line (0) until 101th (0+100) lines, hence 101 lines instead of the asked 100. try
:.,+99dOlivier Dulac– Olivier Dulac2013-08-26 12:40:28 +00:00Commented 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– DemiSheep2014-07-01 15:00:55 +00:00Commented 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,$d
1 denotes the first line and
$ denotes the last line
-
1or
:%d(in ":" context, ":%..." is shortcut for ":1,$...")Olivier Dulac– Olivier Dulac2013-08-26 12:35:57 +00:00Commented Aug 26, 2013 at 12:35 -
also nice:
dGkeys → kill all content from actual [e.g. top line] to bottom lineerch– erch2013-08-26 12:39:27 +00:00Commented 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– vgoff2015-07-21 06:54:05 +00:00Commented 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 herePrvt_Yadav– Prvt_Yadav2018-05-07 06:28:03 +00:00Commented May 7, 2018 at 6:28
-
This answer is identical to the accepted answer.2018-05-07 06:43:13 +00:00Commented May 7, 2018 at 6:43
-
Escape is just in case you are in insert mode.Rida Abid– Rida Abid2018-05-07 08:48:02 +00:00Commented May 7, 2018 at 8:48
echo -n > <file>removes all lines from a file (actually just overwrites the file with "").echoat all for that;> $fileworks just fine.viin an interview?vim-gtkpackage, since that gives you access to the xclipboard within vim), typevimtutorat 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.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 usedvi?"