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.
-
-
-
15
-
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.– MunimAug 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.– MunimSep 13, 2013 at 7:02
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
delete 100 lines backwards from (including) the current one
d99k
delete lines in a specific range by line number
:1,100d
delete 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
:.,+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. 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
-
1
-
also nice:
d
G
keys → kill all content from actual [e.g. top line] to bottom line– erchAug 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.– vgoffJul 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
echo -n > <file>
removes all lines from a file (actually just overwrites the file with "").echo
at all for that;> $file
works just fine.vi
in an interview?vim-gtk
package, since that gives you access to the xclipboard within vim), typevimtutor
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.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
?"