How can I delete multiple lines in vi?

2020-05-11 18:19发布

I have tried to follow the following:

How to delete selected text in VI editor

but

5dd

gives

E492: Not an editor command: 5dd

I then tried:

5d

Which only deletes a single line. How can I delete multiple lines?

标签: vi
12条回答
手持菜刀,她持情操
2楼-- · 2020-05-11 19:02

To delete all the lines use - ESC gg dG To delete few lines lets say 5 then use ESC 5dd

查看更多
放荡不羁爱自由
3楼-- · 2020-05-11 19:02

It could be done as following..

  1. Position the cursor at the place you want to delete.
  2. Press "v" to start the visual selection.
  3. Take the cursor to the position you want to delete using the arrow key.
  4. Press "d" which will delete the lines.
  5. Now save and exit using ":x!"
查看更多
够拽才男人
4楼-- · 2020-05-11 19:10

Sounds like you're entering the commands in command mode (aka. "Ex mode"). In that context :5d would remove line number 5, nothing else. For 5dd to work as intended -- that is, remove five consequent lines starting at the cursor -- enter it in normal mode and don't prefix the commands with :.

查看更多
时光不老,我们不散
5楼-- · 2020-05-11 19:10

If you want to delete a range AFTER a specific line trigger you can use something like this

:g/^TMPDIR/ :.,+11d

That deletes 11 lines (inclusive) after every encounter of ^TMPDIR.

查看更多
Rolldiameter
6楼-- · 2020-05-11 19:11

Commands listed for use in normal mode (prefix with : for command mode).
Tested in Vim.

By line amount:

  • numdd - will delete num lines DOWN starting count from current cursor position (e.g. 5dd will delete current line and 4 lines under it => deletes current line and (num-1) lines under it)
  • numdk - will delete num lines UP from current line and current line itself (e.g. 3dk will delete current line and 3 lines above it => deletes current line and num lines above it)

By line numbers:

  • dnumG - will delete lines from current line (inclusive) UP to line number num (inclusive) (e.g. if cursor is currently on line 5 d2G will delete lines 2-5 inclusive)
  • dnumgg - will delete lines from current line (inclusive) DOWN to the line number num (inclusive) (e.g. if cursor is currently on line 2 d6gg will delete lines 2-6 inclusive)
  • (command mode only) :num1,num2d - will delete lines line number num1 (inclusive) DOWN to the line number num2 (inclusive). Note: if num1 is greater than num2 — vim will react with Backwards range given, OK to swap (y/n)?
查看更多
Anthone
7楼-- · 2020-05-11 19:15

I find this easier

  1. Go VISUAL mode Shift+v
  2. Select lines
  3. d to delete

https://superuser.com/questions/170795/how-can-i-select-and-delete-lines-of-text-in-vi

查看更多
登录 后发表回答