How to delete lines (lines) below the current line in vim?

Is there a command to delete a line (or several lines) that is below the current line? I am currently doing this as: j dd and then . repeat as needed. Is there a team that brings all this together?

UPDATE: The reason I would like to have such a command is because I do not like to move away from the current position, but I delete the lines below.

+47
vim
02 Sep '10 at 6:40
source share
8 answers

The delete ex command will work beautifully.

:+,$d 

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

To delete the next two lines, the next range will work, +1,+2 or the abbreviated +,+2

 :+,+2d 

As @ib, the mentioned command :delete or :d move the cursor to the beginning of the line next to the deleted text. (Even with the installation of nostartofline ). To overcome this, we can execute the normal mode command. `` `` will return to the exact position before the last jump, in which case the command :d . Now our team

: + , + 2 d enter ` `

Or as one ex command

 :+,+2d|norm! `` 

To make this easier, we complete this with the command:

 command! -count=1 -register D :+,+<count>d <reg><bar>norm! `` 

Now, to remove the following next 3 lines:

 :3D 

This command can also take {reg} as :delete and :yank . Thus, deleting the following 4 lines in register a will be:

 :4D a 

For more information

 :h :d :h :command :h :command-register :h :command-count :h `` 
+113
Sep 02 '10 at 19:32
source share

"dG" should work. This means deleting all lines to the end of the file from the current cursor.

+45
Mar 17 '15 at 0:20
source share

This will delete all lines below the current:

 jdG 

Unfortunately, this will move the cursor to the beginning of the current line after deletion.

+25
Sep 02 '10 at 10:14
source share

Ok, for that you just can use the xxdd command. Most of the time I know (at least have an idea) the size of the script that I am editing. So, the commands, as shown below, are usually more than enough:

  • 99dd
  • 999dd to delete 999 lines starting at the cursor position.
  • 9999dd
  • 99999dd a very long script;)
+6
Feb 06 '15 at 15:32
source share

Other solutions are informative, but I believe it would be easier to use a macro for this:

qq (starts recording)

jddk (go down, delete the line and go back up - that is, the thing you want to do)

q (write to end)

Now you can do @q to do this by keeping the cursor at the current position. You can also do something like 5@q to delete 5 lines under the cursor.

And finally, if you repeat the action more than once, you can simply type @@ after the first launch of @q (this repeats the last macro used, in this case q )

+5
Sep 23 '13 at 8:57
source share

This assignment is for grades!

Try maj20dd`a

ma sets the file label to 'a', j20dd does the deletion you want (20 rows in this case), and `a restores you to the label position (row and column).

Obviously, this template can be expanded to do everything you need before returning to the sign. If you use ma (or any other capital letter), the label will be truly unique for different files, so you can even edit it elsewhere before returning. If you have very frequent use, you can make it a macro, as suggested above.

+3
Sep 02 '10 at 13:44
source share

You can enter the number of rows to delete: j 2 0 d d k .

+2
Sep 02 2018-10-09T00:
source share

You can define a small function that does exactly what you described: delete the following lines n below the current line and restore the cursor to its original 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 

In addition, you can define a command that accepts the number of lines below delete (one, by default) and a register name to use as optional (like the command :delete ).

 :command! -range=1 -register -bar D call DeleteNextLines(<count>, <q-reg>) 

In addition, you can define a mapping to run :D , if necessary.

0
Sep 2 '10 at 8:45
source share



All Articles