In vim when my cursor is on the first line I can press:
100dd
to delete the first 100 lines.
But how do I delete all lines except the last 100 lines?
To delete a line in Vi or Vim, switch to normal mode first. If you're into command mode or insert mode, you can switch back to normal mode by pressing Escape. Highlight the line that you want to delete, then hit dd or D on the keyboard. The editor will automatically remove the whole line from the file.
Delete All Lines Press the Esc key to go to normal mode. Type %d and hit Enter to delete all the lines.
In normal mode:
G100kdgg
In other words:
G -> go to last line
100k -> go up 100 lines
dgg -> delete to top of file
In ex mode:
:1,$-100d
Explanation: ":" puts the editor in "ex mode". The d command of ex mode deletes lines, specified as a single line number, or a range of lines. $ is the last line, and arithmetic can be applied to line numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With