Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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.

like image 424
Valentin V Avatar asked Sep 02 '10 06:09

Valentin V


People also ask

How do I delete a specific part of Vim?

Press 'v' to enter a select mode, and use arrow keys to move that around. To delete, press x. To select lines at a time, press shift+v. To select blocks, try ctrl+v.

How do I delete a specific line in vi editor?

To delete a line in Vi or Vim, switch to normal mode first. If you're into command mode or insert mode, you can switch back to normal mode by pressing Escape. Highlight the line that you want to delete, then hit dd or D on the keyboard. The editor will automatically remove the whole line from the file.

How do I delete a line in Vim match pattern?

In order to delete lines matching a pattern in a file using vim editor, you can use ex command, g in combination with d command.


1 Answers

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 :+,+<count>d <reg><bar>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 `` 
like image 165
Peter Rincker Avatar answered Oct 14 '22 09:10

Peter Rincker