tags:

views:

657

answers:

4

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?

+22  A: 

In normal mode:

G100kdgg

In other words:

G     -> go to last line
100k  -> go up 100 lines
dgg   -> delete to top of file
too much php
simple and elegant. I like it!
technomalogical
+26  A: 

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.

Martin v. Löwis
FWIW this is the better answer, IMHO
Nathan Fellman
+3  A: 

Could you elaborate on that?

:1,$-3d

I meant 100 of course, sorry. See the edit for an explanation.
Martin v. Löwis
should be a comment, not an answer
David Claridge
He can't comment, bummer, he has very little recourse other than to watch and hope things get explained.
Evan
Oh right, not enough rep... slightly broken system. I retract my -1
David Claridge
Thanks for the explanation :)
+6  A: 

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.

Greg Hewgill