How do I delete the next line in vim?

10

2

In emacs, whenever I want to delete a few lines of text, I just use C-k until all the text is gone. However, in vim it seems a bit more complex. I know I can do d$ to delete until the end of the line and dd to delete the entire line I'm on, but how do I delete all of the next line?

Jason Baker

Posted 2010-04-16T14:06:40.177

Reputation: 6 382

Answers

5

Like this:

:+1d

Ignacio Vazquez-Abrams

Posted 2010-04-16T14:06:40.177

Reputation: 100 516

16

Assumption: You want to end up at the line you started on.

My answer: jddk

  • j (moves down)
  • dd (deletes current line)
  • k (moves up)

Try it – it's quick! In fact, it's two keystrokes less than the currently accepted answer because:

  1. you don't need to hold [Shift] to enter the colon ':' and plus '+' characters, and
  2. you don't need the implied [Enter] at the end of the sequence, since jddk is entered all in visual mode as opposed to command mode.

Plus, jddk is all on home row of the keyboard.

I spent a long time using h, j, k, l to navigate in vi, long before the terminal emulation software I used started supporting arrow keys. (I'm talking about ~20 years ago ;-)

Chris W. Rea

Posted 2010-04-16T14:06:40.177

Reputation: 10 282

1hjkl is faster anyway. I've never used a keyboard that didn't have arrow keys on it, and I began to greatly value the efficacy of these keymappings about 10 minutes after I stopped being annoyed at the unintuitiveness of them. – intuited – 2010-04-17T22:44:37.350

4

I strongly recommend reading this answer in stack overflow, which got over 500 upvotes: https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim/1220118#1220118 -- the answer is long, but helps to understand why vim is powerful...

K Robinson

Posted 2010-04-16T14:06:40.177

Reputation: 897

Thanks for pointing that out! Very thorough and informative answer about grokking vim and vi. – Chris W. Rea – 2010-04-20T00:11:33.643

2

You may also be interested in visual mode. Just use v to enter and y to yank or d to delete. Checking :help is a great place when you're stuck as well. For instance :help delete will give you the manual for most of the usual delete commands.

Joe Bane

Posted 2010-04-16T14:06:40.177

Reputation: 206

0

If you want to delete a bunch of lines in a row, you can use dd to delete the line you're on, then keep hitting (or hold down) . (period) to repeat the command.

coneslayer

Posted 2010-04-16T14:06:40.177

Reputation: 7 494

or type Ndd where N is the number of lines to delete. say, to delete the current line and the next two lines (3 total), type 3dd – quack quixote – 2010-04-17T10:42:23.787