Vim Delete Operations

October 13, 2025

Abstract

Vim offers a powerful and comprehensive set of delete operations that can handle everything from single characters to large-scale text manipulations. Understanding these commands and their combinations can dramatically improve your text editing efficiency. This guide covers all essential delete operations in Vim, from basic commands to advanced combinations with motions, ranges, and visual modes.

Keywords: Vim, Text Editor, Development Tools, Productivity, Command Line

Character and Word Deletion

Vim provides precise control over character and word-level deletions:

  • x - Delete the character under the cursor
  • X - Delete the character before the cursor (to the left)
  • dw - Delete from cursor position to the end of the current word
  • de - 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 word
  • Ctrl+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 line
  • ndd - Delete n lines starting from the current line (e.g., 3dd deletes the current line and the next 2 lines)
  • D or d$ - Delete from cursor to the end of the line
  • d0 - Delete from cursor to the beginning of the line
  • dG - Delete from cursor to the end of the file
  • dgg or d1G - 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,7d deletes lines 3 through 7)
  • :1,$d or :%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/^$/d deletes all empty lines)

Visual Mode Deletion

Visual modes provide intuitive selection-based deletion:

  • v - Enter visual character mode, select text, then press d to delete
  • V - Enter visual line mode, select lines, then press d to delete
  • Ctrl+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 file
  • dgg - Delete from current position to beginning of file
  • d5w - Delete the next 5 words
  • d3j - Delete 3 lines downward
  • dfx - Delete up to and including the next occurrence of character 'x' on the current line
  • d/^ - 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 quotes
  • da" - Delete content inside double quotes including the quotes
  • di( - Delete content inside parentheses
  • da( - 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/d or :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:

  1. Delete blank lines in a file: :g/^$/d
  2. Delete lines 10 through 20: :10,20d
  3. Delete from current line to line 50: :.50d
  4. Clear entire file: ggdG or :%d
  5. Delete all lines containing "TODO": :g/TODO/d
  6. Delete from cursor to next occurrence of "end": d/end

Tips for Maximum Efficiency

  1. Combine operations: Chain multiple commands for complex manipulations (e.g., ggdG:wq)
  2. Use text objects: Learn i (inner) and a (around) modifiers for semantic deletion
  3. Leverage visual mode: When the target is hard to specify with motions, use visual mode for precise selection
  4. Create mappings: For frequently used patterns, create custom key mappings
  5. 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.