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 18:51

If you prefer a non-visual mode method and acknowledge the line numbers, I would like to suggest you an another straightforward way.

Example

I want to delete text from line 45 to line 101.

My method suggests you to type a below command in command-mode:

45Gd101G

It reads:

Go to line 45 (45G) then delete text (d) from the current line to the line 101 (101G).

Note that on vim you might use gg in stead of G.

Compare to the @Bonnie Varghese's answer which is:

:45,101d[enter]

The command above from his answer requires 9 times typing including enter, where my answer require 8 - 10 times typing. Thus, a speed of my method is comparable.

Personally, I myself prefer 45Gd101G over :45,101d because I like to stick to the syntax of the vi's command, in this case is:

+---------+----------+--------------------+
| syntax  | <motion> | <operator><motion> |
+---------+----------+--------------------+
| command |   45G    |        d101G       |
+---------+----------+--------------------+
查看更多
成全新的幸福
3楼-- · 2020-05-11 18:54

You can delete multiple(range) lines if you know the line numbers:

:[start_line_no],[end_line_no]d

Note: d stands for delete

where,
start_line_no is the beginning line no you want to delete and end_line_no is the ending line no you want to delete. The lines between the start and end, including start and end will be deleted.

Eg:

:45,101d

The lines between 45 and 101 including 45 and 101 will be deleted.

查看更多
啃猪蹄的小仙女
4楼-- · 2020-05-11 18:54
  1. Esc to exit insert mode
  2. :1enter go to line 1 (replace '1' with the line you are interested in)
  3. 5dd delete 5 lines (from the current line)

Type :set number (for numbered lines).

查看更多
贼婆χ
5楼-- · 2020-05-11 18:58

it is dxd, not ddx

if you want to delete 5 lines, cursor to the beginning of the first line to delete and d5d

查看更多
家丑人穷心不美
6楼-- · 2020-05-11 19:01

Press the Esc key to make sure your are not in an edit mode. Place the cursor on the first line to be deleted. Enter :5dd. The current line, and the next four lines should be deleted.

Alternately, if you have line numbering turned on...

Press the Esc key to make sure your are not in an edit mode. Enter :#,#d where '#' stands for the beginning and ending line numbers to be deleted.

查看更多
【Aperson】
7楼-- · 2020-05-11 19:02

d5d "cuts" five lines

I usually just throw the number in the middle like:

d7l = delete 7 letters

查看更多
登录 后发表回答