How can I delete multiple lines in vi?

后端 未结 11 1136
天命终不由人
天命终不由人 2021-01-29 20:53

I have tried to follow the following:

How to delete selected text in VI editor

but

5dd

gives

E492: Not a

11条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 21:40

    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  |  |  |
    +---------+----------+--------------------+
    | command |   45G    |        d101G       |
    +---------+----------+--------------------+
    

提交回复
热议问题