Character and Word Deletion
Vim provides precise control over character and word-level deletions:
x- Delete the character under the cursorX- Delete the character before the cursor (to the left)dw- Delete from cursor position to the end of the current wordde- Delete from cursor to the end of the current word (excluding separators)daw- Delete the entire word under the cursor (including surrounding whitespace)diw- Delete the word under the cursor (excluding whitespace)db- Delete the previous word (backwards)
In insert mode, you can also use:
Ctrl+w- Delete the previous wordCtrl+u- Delete to the beginning of the line
Line and Multi-line Deletion
For line-based operations, Vim offers these efficient commands:
dd- Delete the current linendd- Delete n lines starting from the current line (e.g.,3dddeletes the current line and the next 2 lines)Dord$- Delete from cursor to the end of the lined0- Delete from cursor to the beginning of the linedG- Delete from cursor to the end of the filedggord1G- Delete from cursor to the beginning of the file
Range-based Deletion
Vim's command-line mode allows for powerful range-based deletions:
:n1,n2d- Delete lines from n1 to n2 (e.g.,:3,7ddeletes lines 3 through 7):1,$dor:%d- Delete all content in the file (where % is Vim's wildcard, equivalent to 1,$, so :%d is actually a shorthand version of :1,$d):.,$d- Delete from current line to the end of the file:g/pattern/d- Delete all lines matching the pattern (e.g.,:g/^$/ddeletes all empty lines)
Visual Mode Deletion
Visual modes provide intuitive selection-based deletion:
v- Enter visual character mode, select text, then pressdto deleteV- Enter visual line mode, select lines, then pressdto deleteCtrl+v- Enter visual block mode for column-based selections
Advanced Motion Combinations
The real power of Vim's delete operations comes from combining d with motion commands:
Basic Motion Combinations
The d{motion} pattern allows flexible range specification:
dG- Delete from current position to end of filedgg- Delete from current position to beginning of filed5w- Delete the next 5 wordsd3j- Delete 3 lines downwarddfx- Delete up to and including the next occurrence of character 'x' on the current lined/^- Delete to the beginning of the line
Text Object Deletion
Vim's text objects provide semantic deletion capabilities:
diw- Delete inner word (excluding whitespace)daw- Delete a word (including whitespace)di"- Delete content inside double quotesda"- Delete content inside double quotes including the quotesdi(- Delete content inside parenthesesda(- Delete content inside parentheses including the parentheses
This pattern works with various text objects: ", ', `, (, [, {, <, and tags.
Batch and Pattern-based Operations
For large-scale text manipulation, Vim provides powerful batch operations:
Pattern Matching
:g/pattern/d- Delete all lines containing the pattern:g!/pattern/dor:v/pattern/d- Delete all lines NOT containing the pattern:g/^$/d- Delete all empty lines
Command Combinations
ggdG- Go to the first line and delete to the end (clear entire file)ggdG:wq- Clear file content, save, and quit in one sequence:normal! ggdG- Execute normal mode commands across selected ranges
Custom Mappings
You can create custom key mappings for frequently used deletion patterns:
:map <key> <commands>- Create custom shortcuts for complex deletion operations- Example:
:map ^P I#<ESC>- Comment the current line with a single keystroke
Practical Examples
Here are some practical scenarios and their solutions:
- Delete blank lines in a file:
:g/^$/d - Delete lines 10 through 20:
:10,20d - Delete from current line to line 50:
:.50d - Clear entire file:
ggdGor:%d - Delete all lines containing "TODO":
:g/TODO/d - Delete from cursor to next occurrence of "end":
d/end
Tips for Maximum Efficiency
- Combine operations: Chain multiple commands for complex manipulations (e.g.,
ggdG:wq) - Use text objects: Learn
i(inner) anda(around) modifiers for semantic deletion - Leverage visual mode: When the target is hard to specify with motions, use visual mode for precise selection
- Create mappings: For frequently used patterns, create custom key mappings
- Practice motion commands: The more comfortable you are with motion commands, the more powerful your delete operations become
Conclusion
Vim's delete operations are far more than simple text removal commands. By combining delete commands with motions, text objects, ranges, and visual modes, you can efficiently handle everything from single character edits to large-scale batch processing. Mastering these techniques transforms Vim into an incredibly powerful tool for text manipulation, code refactoring, log processing, and configuration file management. The investment in learning these patterns pays dividends in long-term productivity and editing speed.