Edward Tanguay
Edward Tanguay

Reputation: 193362

In vim, how can I delete all lines in a file 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?

Upvotes: 22

Views: 13522

Answers (3)

Martin v. Löwis
Martin v. Löwis

Reputation: 127527

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.

Upvotes: 51

Greg Hewgill
Greg Hewgill

Reputation: 993961

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.

Upvotes: 11

too much php
too much php

Reputation: 91048

In normal mode:

G100kdgg

In other words:

G     -> go to last line
100k  -> go up 100 lines
dgg   -> delete to top of file

Upvotes: 56

Related Questions