tags:

views:

228

answers:

5

Is there a command to delete a line (or several lines) that is immediately below current line? Currently I'm doing it as: jdd and then . to repeat as needed. Is there a command that would combine all these?

UPDATE: The reason I would like to have such command is that I don't like to move away from current position, yet be able to delete lines below.

+3  A: 

You could define a little function that does exactly what you described: delete the next number lines below the current line and restore initial cursor position.

function! DeleteNextLines(number)
    let line = line('.')
    let cnt = min([a:number, line('$') - line])
    if 0 < cnt
        let col = col('.')
        execute 'normal! j' . cnt . 'dd'
        call cursor(line, col)
    endif
endfunction

Also, I recommend defining a command that accepts count and call this function with appropriate number of lines to delete:

command! -range=1 DD call DeleteNextLines(<count>)

(Additionally you could define a mapping for :DD, if it's necessary.)

ib
Haven't tested that but accept as answer, thank you.
Valentin Vasiliev
+1  A: 

You could enter the number of lines to delete: j 20 dd k.

Paul Ruane
Final `k` moves the cursor above the initial line. Also, cursor column position will be lost because of `dd`.
ib
@ib: Not in my tests it doesn't. Have you actually tried it? Note the initial **j**.
Paul Ruane
I'm sorry, my first point was wrong (when I've tried the command out, I have not noticed that the count I specified ('20', in your example) was greater than the number of lines till the end of the buffer). But the second one remains: column position of the cursor changes after `dd` (to the first non-blank character of the line next to the deleted lines, I believe).
ib
@ib: Yes, I agree the column position changes.
Paul Ruane
Please make an insignificant edit of your answer, so I can upvote it. I think your answer does not deserve a negative rank after all.
ib
These commands indeed return cursor to the original line, but not to the original position. Thank you for your answer.
Valentin Vasiliev
+1  A: 

This will delete ALL lines below the current one:

jdG

Unfortunately that will move the cursor to the beginning of current line after the deletion is made.

ClosedID
And will delete every line till the end of the buffer, too.
ib
I misread the original question, sorry. Thought the OP wants to delete ALL lines below the current one. Changed my post.
ClosedID
+3  A: 

This is a job for marks!

Try maj20dd`a

ma sets the file-specific mark 'a', j20dd does the deletion you want (20 lines in this case), and `a restores you to the mark's position (line and column).

Obviously this pattern can be extended to do anything you want before returning to the mark. If you use mA (or any other capital letter) the mark will actually be unique across files, so you can even edit elsewhere before returning. If you have a very frequent usage you could make it a macro as suggested above.

Alex Feinman
+6  A: 

The delete ex command will work nicely.

:+,$d

This will delete all the lines from current +1 till the end ($)

For more information

:h :d
Peter Rincker
Note that this is a fairly easy solution that works "out of the box" and is generalizable to other commands (for example, `:+1,+5s!a!b!g` will replace a's with b's on the 4 lines after the cursor). See :help command-ranges for some more examples.
David Winslow