Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vim, how can I delete all lines in a file except the last 100 lines?

Tags:

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?

like image 317
Edward Tanguay Avatar asked Aug 10 '09 00:08

Edward Tanguay


People also ask

How do I delete a range of lines in Vim?

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.

How do I delete 1000 lines in vi?

Delete All Lines Press the Esc key to go to normal mode. Type %d and hit Enter to delete all the lines.


2 Answers

In normal mode:

G100kdgg

In other words:

G     -> go to last line
100k  -> go up 100 lines
dgg   -> delete to top of file
like image 167
too much php Avatar answered Oct 07 '22 04:10

too much php


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.

like image 45
Martin v. Löwis Avatar answered Oct 07 '22 04:10

Martin v. Löwis