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)?

17
2dk

same effect as 3dd but upwards.

2
  • 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
    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'. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.