Vim, delete to row n

4

1

I'm often editing files where i have to delete everything except the five first and the three last rows. I do that by moving to row six and type ":,.$-3 d". Is there any way accomplish the same using d and some kind of motion?

johnny

Posted 2010-11-16T08:31:27.853

Reputation: 775

Answers

5

Sure is! d[num]G where [num] is the destination line number. Any other movement command will also work rather than G, of course.

To delete the first five and last three rows, assuming you've just opened the file and are on the first line, it would be:

d5G   # delete from current position to line 5
G     # jump to last line
d2k   # delete from current position to 2 lines up (3 lines total)

Edit: Just reread the question and saw I got your intentions backwards - you want to keep the first five/last three, not delete them. In that case, it's:

G     # jump to last line
3k    # up 3 lines
d6G   # delete from current position to line 6

Dave Sherohman

Posted 2010-11-16T08:31:27.853

Reputation: 5 143

Thank you for your answer. I always tried to do it the other way around, positioning myself at the sixth row trying to move(delete) to row $-3. – johnny – 2010-11-16T09:08:30.290