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.
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
compatible option is on or not.
delete 100 lines forward from (including) the current one
repeat dd (delete current line) 100 times:
delete from current line to 99 lines forward
delete 100 lines backwards from (including) the current one
delete lines in a specific range by line number
delete lines in a range beginning with the current line
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
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 matter where are you are in the file, you can do
:%d would do better in less key presses. Well, counting the
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