如何在 vim 中删除当前行下面的行?

是否有一个命令来删除紧接在当前行下面的一行(或几行) ? 目前,我是这样做的: jdd,然后 .根据需要重复。 有没有一个命令可以把所有这些结合起来?

更新: 我想有这样的命令的原因是,我不喜欢从当前位置移动,但能够删除行以下。

118012 次浏览

Just for the fun of it, you can define a little function that does exactly what you described: deletes the next n lines below the current line and restores the 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 number of lines to delete (one, if omitted) and the register name to use as an optional argument (just like the :delete command).

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

Additionally, you can define a mapping for triggering the above :D command, if it is necessary.

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

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.

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.

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 ``

The other solutions are informative, but I feel it'd be simpler to use a macro for this:

qq (begins recording)

jddk (go down, delete the line, and go back up - i.e. the thing you want to do)

q (end recording)

Now you can do @q to perform this action, maintaining the cursor at the current position. You could also do something like 5@q to delete 5 lines below the cursor.

And finally, if you're repeating the action more than once, you could just type @@ after the first time you run @q (this repeats the last used macro - in this case q)

well, to do it simply you could use the xxdd command. Most of the time I know (at least have an idea) the size of the script I am editing. So, the command as below is usually more than enough :

  • 99dd
  • 999dd to remove 999lines starting at the cursor position.
  • 9999dd
  • 99999dd for very long script ;)

dG should work.
This means delete all rows until end of file from current cursor.