148

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?

1
  • 2
    As far as I recall, 5dd should be valid syntax and should delete 5 lines starting with the one the cursor is on. Any chance CAPS LOCK is on and you use a keyboard layout that causes the numeric keys to shift when it is?
    – Eric J.
    Commented Apr 9, 2013 at 21:27

11 Answers 11

199

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
162

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.

3
  • 18
    You can use $ to denote the last line in the file too. So :45,$d would delete every line from 45 until the end. Commented May 25, 2017 at 8:45
  • You can even use Patterns like /<\/div>/
    – Björn
    Commented Jun 4, 2018 at 9:22
  • 3
    If you want to start the range from the lne you are currently on you can do :.,[end_line_no]d
    – Joe
    Commented Jan 8, 2020 at 18:11
142

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

21

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)?
3
  • 1
    This helped me because I didn't realize num 'd' 'd' was trying to delete below cursor. I had my cursor at bottom of the file trying to delete the lines above my cursor! thanks for the help! Commented Feb 12, 2018 at 16:23
  • Deleting by linenumber: ':<start>,$d' will delete until the end of the file.
    – Devolus
    Commented Jun 8, 2020 at 8:22
  • Can it be used to delete from current line until find a pattern? Lets say I want to delete N lines but I don't want to count, and I visualize a unique word that I could delete from current until that line with that unique word.
    – DrBeco
    Commented Apr 4 at 13:12
12

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       |
+---------+----------+--------------------+
9

d5d "cuts" five lines

I usually just throw the number in the middle like:

d7l = delete 7 letters

7

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.

2
  • I do not think that it works with colon at the beginning.
    – Outcast
    Commented Oct 22, 2018 at 9:47
  • @PoeteMaudit Those are valid vi commands. The colon causes vi to move the cursor to the bottom of the screen and accept a line editor command.
    – Kenster
    Commented Oct 22, 2018 at 13:41
6
  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).

3

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

it is dxd, not ddx

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

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.