8

We can delete lines after the cursor (e.g.: the next 3 lines) with:

3dd

But how can we delete the lines before the cursor? (e.g.: 3 lines before cursor)?

LanceBaynes
  • 40,135
  • 97
  • 255
  • 351

2 Answers2

17
2dk

same effect as 3dd but upwards.

forcefsck
  • 7,964
  • 2
    While the '2' might sound counterintuitive you need to remember that when deleting upwards it starts with the line the cursor is on, /then/ follows the move command. This may sound weird, but it is in fact consistent with how vi handles commands. – Shadur-don't-feed-the-AI Mar 14 '11 at 10:31
  • 3
    Actually, it's not just because you're deleting upwards, it's because the structure of the command is different. 'dd' means delete this line. 3dd means 'delete this line'x3. On the other hand 'dk' means 'delete from here to one line up' - that means two lines. So '2dk' means delete from here to two lines up'. By the way, you can delete downwards the same: '2dj' will also delete three lines - it's the same as '3dd'. – Shawn J. Goff Mar 14 '11 at 12:06
0

I use solution similar to @forcefsck's answer, but IMO slightly more consistent.

d2k

same as 2dk.

I use mostly two kind of yank/delete(kill) command,

  • d + motion, eg, d), d2j, d3e, d0
  • d + text-object, eg d2aw, dib, da), dat, das, dip.

for some common text-object, see

:h v_is

and

:h )

as two example.

qeatzy
  • 236