How can I delete multiple lines in vi?

后端 未结 11 1131
天命终不由人
天命终不由人 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:17

    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.

    0 讨论(0)
  • 2021-01-29 21:21
    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).

    0 讨论(0)
  • 2021-01-29 21:23

    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.

    0 讨论(0)
  • 2021-01-29 21:27

    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.

    0 讨论(0)
  • 2021-01-29 21:31

    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 :.

    0 讨论(0)
  • 2021-01-29 21:35

    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

    0 讨论(0)
提交回复
热议问题