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?
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?
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.
An alternative general purpose solution:
:%!tail -100
You can use any shell command after the !
to arbitrarily modify the current buffer. Vim starts the command and feeds the current file to stdin
, and reads the new buffer from stdout
.