Vim may be used to create and modify text and computer program files for different reasons. When processing text files in the Vim editor, you frequently have to remove one or more lines, and it allows you to easily modify your files, as does any other text editor. As there are various methods to delete a line in this best text editor listed below. This article concentrates on helping you with the VIM delete line command or how to delete multiple lines or Vim delete all lines.
Quick Answer: Vim Delete Commands
If you just want the commands, here they are. Delete the current line with dd, delete 5 lines with 5dd, delete all lines with :%d, delete blank lines with :g/^$/d, and delete lines 3 through 7 with :3,7d. In normal mode, commands like dd work immediately and do not need Enter. Ex commands like :%d do need Enter.
| Task | Command |
| Delete current line | dd |
| Delete 5 lines | 5dd |
| Delete lines 3 to 7 | :3,7d |
| Delete all lines | :%d |
| Delete blank lines | :g/^$/d |
| Delete lines matching "error" | :g/error/d |
| Delete from current line to end of file | dG |
| Delete from current line to beginning of file | d1G |
How to delete a single line in Vim editor?
The standard way to delete a line in Vim or Vi is dd. Press Esc first to make sure you're in normal mode, move the cursor to the line you want, then type dd.
No Enter needed. That part trips people up all the time.
Use dd in normal mode
If you're editing text and nothing seems to happen, you're probably still in insert mode. Hit Esc, then try dd again. Normal-mode commands are immediate.
ddWhat happens after you delete a line
By default, Vim stores deleted text in a register. So dd doesn't just remove the line — it also makes it available for pasting with p. That's handy until you accidentally overwrite something you meant to keep.
If you want a deeper explanation this guide is worth a read: Copy, Cut & Paste in Vim Registers are one of those topics beginners skip, then run into later.
How to delete a Single Word in vim/vi editor
- To return to normal mode, use the Esc key.
- To erase a term, place your cursor at the beginning of the term you want to remove.
- Make the following keystrokes: dw
Delete one character with x
x deletes the character under the cursor.
xDelete previous character with X
X deletes the character before the cursor. Think of it like a backward delete.
XDelete a word with dw
dw deletes from the cursor to the start of the next word. It's one of those commands you end up using constantly.
dwDelete to end of word with de
de deletes from the cursor to the end of the current word. Slightly different feel than dw, especially around punctuation.
deDelete to end of line with D
D is not the same as dd. It deletes from the cursor position to the end of the current line, not the whole line.
DDelete a visual selection with d
In visual mode, select text first, then press d. This works in character-wise visual mode, line-wise visual mode, and block-wise mode.
v
...move cursor...
dHow to remove multiple lines in vim?
There are three practical ways to delete multiple lines in Vim: count-based deletion, line ranges, and visual line mode. Which one feels best depends on whether you know the line numbers already.
Using ndd
Prefix the dd command and mention the number of lines to be erased to delete several lines at once. Type a number before dd to delete that many lines starting from the current line. So 5dd deletes the current line plus the next 4 lines. Not 5 lines after the cursor line — 5 total.
5dd
10dd
Using line ranges with :start,endd
If you know the exact line numbers, Ex mode is cleaner. For example, :3,7d deletes lines 3 through 7.
This is where it helps to display line number in vim. I personally turn line numbers on before any bulk edit. Saves mistakes.
:3,7d
:10,20dUsing visual line mode V
Don't know the line numbers? Use visual line mode. Press V to start selecting whole lines, move up or down with the cursor keys or j/k, then press d.
This is a good method when you're reviewing a block by eye. Also useful if you first want to select all text in Vim and then trim it down from there.
V
j
j
dHow to Remove a group of lines from a document
In Vim, press the Esc key to return to normal mode. When executing the following command, replace [from] and [to] with the line numbers where you want the array to start and end, respectively.
:[from],[to]dFor example, if you wanted to erase lines 3,4,5,6 and 7, you'd type:
:3,7dHow to Delete all Lines
This section matters because a lot of people aren't actually trying to delete one line. They want to clear the whole file fast. In Vim, you've got a few solid ways to do that.
:%d
This is the most common Ex command for deleting all lines in Vim or Vi. The % means the entire file, and d means delete.
:%dType it, then press Enter. Unlike dd, this one does require Enter because it's a command-line mode instruction.
:1,$d
This does the same thing in a more explicit way. It says: from line 1 to the last line, delete.
:1,$dIf you're learning ranges anyway, this version makes the syntax easier to understand. Same result, just more literal.
Delete all without copying to a register: :%d_
Normally, deleting all lines yanks that text into a register. If you don't want that, use the black hole variant:
:%d_You can also do this in normal mode with gg"_dG. That sends the deleted text into the black hole register instead of clobbering the unnamed register. Very useful when you've got something important copied already.
After clearing a file, you may want to save changes after deleting lines in Vim so you don't lose the edit or accidentally exit without writing.
How to Delete Pattern-Containing Lines
This is where Vim starts feeling like a real power tool. If you need to remove lines containing a word, tag, error code, or regex pattern, the global command does the job fast.
Delete matching lines with :g/pattern/d
To delete every line containing the word error, run:
:g/error/dYou can swap error for any pattern. This is great for logs, config cleanup, and mass editing.
Delete non-matching lines with :g!/pattern/d
Want to keep only matching lines and remove everything else? Use the inverted form:
:g!/error/dThis deletes all lines that do not contain error.
Use regex safely
Regex works here, which is powerful and a bit dangerous. Test your pattern mentally before you hit Enter, especially on production config files. I've seen a sloppy pattern wipe more than intended.
Some useful examples:
:g/^#/d
:g/^[0-9]/d
:g/\v(error|warning)/dHow to Delete blank lines in VIM/VI
Blank lines are easy. Whitespace-only lines need a slightly different pattern. And yes, this matters — log files and copied configs often have both.
Remove empty lines with :g/^$/d
This deletes lines that contain nothing at all between the start and end of the line.
:g/^$/d
Remove lines containing only spaces
If a line contains spaces or tabs, ^$ won't match it. Use this instead:
:g/^\s*$/dThe regex says: start of line, any amount of whitespace, end of line. Beginner-friendly version? It matches lines that look empty even if they aren't technically empty.
How to delete lines in Insert mode
To begin deleting lines in Vim/Vi, you must first enter insert mode. This can be done by pressing the i key on your keyboard.
Once you are in insert mode, you can delete lines by using the backspace key. This will delete the characters to the left of the cursor, including any spaces or tabs.
After deleting the desired lines, make sure to press the Esc key to exit insert mode and return to normal mode. This will allow you to save your changes or continue editing your document as needed.
Yes, you can hit Backspace or Delete in insert mode, but that's character editing, not the best way to delete lines in a terminal editor. For real line operations, go back to normal mode or use Ex commands.
How to delete lines in Normal mode
By following these three steps, you can easily delete lines in normal mode in Vim/Vi editor.
First, you must move the cursor to the line you want to delete. This can be done by using the arrow keys on your keyboard or by typing the line number and then pressing the G key.
Once the cursor is on the desired line, type the command dd to delete the line. The line will be removed, and the cursor will move to the line directly below the deleted line.
If you accidentally deleted the wrong line, or if you need to undelete the previously deleted line, you can use the u command. Pressing u will undo the last change made in the document.
How to delete lines in Ex mode
By following these three steps, you can easily delete lines in ex mode in Vim or Vi editor. This function can be useful when you need to quickly delete multiple lines of text in a document.
To delete lines in ex mode, you must first enter ex mode. This can be done by typing : (colon) in normal mode and then typing ex command, such as 1,$d to delete all lines in the document.
After typing the Ex command to delete the line(s), press Enter to execute the command. The line(s) will be deleted, and the cursor will move to the next line.
How to use deletion as a means of searching
The method to search and delete line in Vim is quite simple. Follow the steps below to learn how to do it.
To start editing a document in Vim/Vi, open the text file in the terminal and type vi or vim followed by the file name. Press Enter to enter the editor.
Once you are in Vim/Vi, enter the deletion command by typing /. This will search for the word or phrase you want to delete and highlight it in the document.
To delete the highlighted word or phrase, press the x key. This will delete the text from the document and close the search bar. You can then repeat this process to delete additional instances of the same word or phrase.
How to delete from current line to the end of file
If you need to remove everything after the cursor line, use a motion-based delete. This is faster than selecting manually.
Delete to end of file with dG
dG deletes from the current cursor position through the end of the file. If your cursor is at the start of the line, it effectively removes the current line and everything below it.
dGThis is one of my favorites for trimming generated files or cutting off a broken tail section.
How to delete from current line to the beginning of file
The reverse operation is just as useful when you want to keep the lower part of a file and remove everything above.
Delete to beginning of file with d1G
d1G deletes from the current position back to line 1. Again, where your cursor sits on the line matters a little. If you want to remove full lines cleanly, place the cursor near the start of the current line first.
d1GHow to remove lines based on Regular Expressions
- Open the file you want to edit in Vim/Vi by typing in the command:
vi filename. - Press the colon key (:) to enter the command-line mode.
- Type in the command:
g/regexp/dwhere regexp is the regular expression you want to delete. This command will delete all lines that match the given regular expression.
Note: If you only want to delete lines that match a regular expression in a certain range of lines, you can specify the range by typing in the command: start,endg/regexp/d where the start and end are the line numbers of the range.
Common Mistakes When Deleting in Vim
Most deletion problems in Vim aren't bugs. They're mode issues, motion misunderstandings, or register surprises.
dd vs D
dd deletes the whole current line. D deletes from the cursor to the end of that line. Tiny difference in spelling, very different result.
Why Enter is not required for normal-mode commands
dd, 5dd, x, and dw are normal-mode commands, so they execute as soon as you type them. Commands like :%d and :g/^$/d are Ex commands, so they need Enter.
Deleted text goes to registers
By default, deleted text is yanked into the unnamed register. That's useful for pasting, but annoying if you just copied something valuable and then overwrite it with a throwaway delete.
Avoid overwriting the unnamed register
Use the black hole register when you want to delete without copying:
"_dd
"_dG
:%d_If registers still feel fuzzy, the article on how deleted text is stored in Vim registers explains the behavior clearly.
How to Undo a Delete in Vim
You will eventually delete the wrong line. Everyone does. The good news is recovery is easy if you know the two commands below.
Undo with u
Press u in normal mode to undo the last change, including a deleted line, range, or pattern-based delete.
uRedo with Ctrl-r
If you undo too far, press Ctrl-r to redo.
Ctrl-rFor a fuller walkthrough, here's how to undo a deleted line in Vim and redo it if needed. Handy page to bookmark, honestly.
Vim Delete Commands by Mode: Normal vs Command-Line Mode
A lot of confusion comes from mode mixing. Vim has several modes, but line deletion mostly happens in normal mode, visual mode, or command-line mode. Insert mode isn't really where this shines.
Normal mode
This is where commands like dd, 5dd, dG, d1G, x, and dw live. Press Esc to get here if needed.
Visual mode
Use visual mode when you want to select first and delete second. For whole lines, V is usually the best choice.
Command-line/Ex mode
This is for commands starting with :, like :%d, :3,7d, and :g/pattern/d. These require pressing Enter to execute.
Difference Between Vim and Vi Behavior
For the commands covered here, Vim and traditional Vi are mostly aligned. dd, :%d, ranges like :3,7d, and motions like dG are all standard enough that you can use them in both in most environments.
Where things can differ is regex behavior, extended features, defaults, and clipboard integration. If you're curious about the broader difference between Vi and Vim, that comparison goes deeper.
And if you're still deciding between editors, there's also a practical look at Vim vs Nano. Different tools, different comfort levels.
Vim Delete Command Cheat Sheet
If you want one section to bookmark, this is it. It's the quickest reference for deleting lines, words, ranges, blank lines, and pattern matches in Vim or Vi.
| Task | Command |
| Delete current line | dd |
| Delete 5 lines | 5dd |
| Delete line range 10 to 20 | :10,20d |
| Delete all lines | :%d |
| Delete all lines with explicit range | :1,$d |
| Delete all lines without yanking | :%d_ |
| Delete to end of line | D |
| Delete one character | x |
| Delete previous character | X |
| Delete one word | dw |
| Delete to end of word | de |
| Delete current line to end of file | dG |
| Delete current line to beginning of file | d1G |
| Delete empty lines | :g/^$/d |
| Delete whitespace-only lines | :g/^\s*$/d |
| Delete lines matching pattern | :g/pattern/d |
| Delete lines not matching pattern | :g!/pattern/d |
| Undo delete | u |
| Redo delete | Ctrl-r |
| Delete line without yanking | "_dd |
Conclusion
Vim is one of the best and most popular editors in the Linux operating system. It's a text editor that's free, well-documented, and configurable. In this tutorial, we have learned how to delete line in Vim editor and how to delete single words, multiple lines, or how to delete a line in VI. Vim is a powerful text editor with many more capabilities to discover. If you're exploring other options, check out our roundup of the best Linux text editors for different workflows. You can also read our detailed comparison between two popular text editors that are Vim vs. Nano text editors.
If you're comfortable with deletion now, the next useful habit is learning what to do right after: undo, paste, save, quit, and move around faster. That's usually where Vim starts to feel less awkward and more efficient. You can keep going with more essential Vim commands. That pairs nicely with this page once the delete basics are locked in. And if you want a dedicated environment to practice without risking your local machine, a Linux VPS gives you full root access and a safe sandbox to sharpen your skills.
People also read:
I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.