How to delete line(s) below current line in vim?

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

You can define a little function that does exactly what you described: delete the next n lines below the current line and restore initial cursor position. Function! DeleteNextLines(n, reg) let l = line('.') let m = min(a:n, line('$') - l) if m > 0 let c = col('.

') exe '+,+'. M 'd' a:reg call cursor(l, c) endif endfunction Also, you can define a command that accepts the count of lines below to delete (one, by default) and the register name to use as an optional argument (just as :delete command). :command!

-range=1 -register -bar D call DeleteNextLines(, ) Additionally you could define a mapping for running :D, if it's necessary.

Haven't tested that but accept as answer, thank you. – Valentin Vasilyev Sep 2 '10 at 10:58.

The delete ex command will work nicely. :+,$d This will delete all the lines from current +1 till the end ($) To delete the next 2 lines the follow range would work, +1,+2 or shorthand +,+2 :+,+2d As @ib mentioned the :delete or :d command will move the cursor to the start of the line next to the deleted text. (Even with nostartofline set).

To overcome this we can issue the `` normal mode command. `` will jump back to the exact position before the last jump, in this case the :d command. Our command is now :+,+2denter`` Or as one ex command :+,+2d|norm!

`` To make this easier we wrap this all up in a command: command! -count=1 -register D :+,+d norm! `` Now to delete the next following 3 lines: :3D This command can also take a {reg} like :delete and :yank do.So deleting the next 4 lines into register a would be: :4D a For more information :h :d :h :command :h :command-register :h :command-count :h ``.

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 Sep 3 '10 at 1:06 This straightforward solution would be great, if only it does not move the cursor.At least, the command should look like :+,$d|norm!

``. By the way, as stated in the question, author of the question would like to delete several lines below the current one, not necessarily all way down to the end of file. – ib.

Oct 22 at 1:39 @ib: Would the answer be improved by a more detailed explanation of ranges? Maybe set sol? How to use the ` mark?

– Peter Rincker Oct 22 at 2:09 @Peter: Probably, it would be. By the way, turning sol off alone does not solve the issue: when one deletes, say, three lines using :+,+3d, the cursor is moved to the line next the deleted ones. – ib.

Oct 22 at 2:47 @ib: I have incorporated your suggestions into the my answer. Thank you for your feedback. – Peter Rincker Oct 22 at 3:49.

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.

2 And will delete every line till the end of the buffer, too. – ib. Sep 2 '10 at 10:42 I misread the original question, sorry.

Thought the OP wants to delete ALL lines below the current one. Changed my post. – ClosedID Sep 2 '10 at 11:13.

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.

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

Final k moves the cursor above the initial line. Also, cursor column position will be lost because of dd. – ib.

Sep 2 '10 at 9:08 @ib: Not in my tests it doesn't. Have you actually tried it? Note the initial j.

– Paul Ruane Sep 2 '10 at 9:24 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.

Sep 2 '10 at 9:34 @ib: Yes, I agree the column position changes. – Paul Ruane Sep 2 '10 at 9:44 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. Sep 2 '10 at 9:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions