I’ve only started a venture into Neovim. With AI producing a lot of the code I need to get started, and my recent testing with Claude Code, I’m finding I’m back in the terminal a lot.
And while I’ve been here it has made me think of editing with Vim.
Therefore, any posts you see in here about Neovim are from this initial foray into the space, and the way I learn is by sharing what I’ve learned.
Mastering Delete in Neovim: A Beginner’s Guide
If you’re new to Neovim (or Vim), one of the first things you’ll need to master is how to delete text efficiently. Unlike traditional text editors where you simply highlight and press the Delete key, Neovim offers a powerful set of deletion commands that can dramatically speed up your editing once you learn them.
Understanding Neovim Modes
Before diving into delete commands, it’s crucial to understand that Neovim operates in different modes:
- Normal Mode: Where you navigate and execute commands (default mode)
- Insert Mode: Where you actually type text (press
i
to enter) - Visual Mode: Where you select text (press
v
to enter)
Most delete commands work in Normal mode, so press Esc
to ensure you’re there before trying these examples.
Basic Character and Line Deletion
Deleting Single Characters
The most basic delete command is x
, which deletes the character under the cursor:
Original: Hello World
Cursor on 'W' → press x
Result: Hello orld
To delete the character before the cursor, use X
(capital X):
Original: Hello World
Cursor on 'W' → press X
Result: HelloWorld
Deleting Entire Lines
To delete an entire line, use dd
:
Original:
Line 1
Line 2 ← cursor here
Line 3
After pressing dd:
Line 1
Line 3
The Powerful ’d’ Command
The d
command is your Swiss Army knife for deletion. It follows the pattern d + motion
, where motion tells Neovim what to delete.
Common Motion Commands
w
- to the beginning of the next worde
- to the end of the current wordb
- to the beginning of the previous word$
- to the end of the line0
- to the beginning of the lineG
- to the end of the filegg
- to the beginning of the file
Delete + Motion Examples
Delete to end of word (dw
):
Original: function calculateTotal() {
Cursor on 'f' → press dw
Result: calculateTotal() {
Delete to end of line (d$
):
Original: const name = "John Doe";
Cursor on 'n' in "name" → press d$
Result: const na
Delete to beginning of line (d0
):
Original: const name = "John Doe";
Cursor on '=' → press d0
Result: = "John Doe";
Working with Multiple Lines
Delete Multiple Lines
You can delete multiple lines by prefixing dd
with a number:
Original:
function example() {
let a = 1; ← cursor here
let b = 2;
let c = 3;
return a + b + c;
}
Press 3dd to delete 3 lines:
function example() {
return a + b + c;
}
Delete from Current Line to End of File
Use dG
to delete from the current line to the end of the file:
Original:
Line 1
Line 2 ← cursor here
Line 3
Line 4
After pressing dG:
Line 1
Advanced Deletion with Text Objects
Neovim includes powerful text objects that let you delete logical units of text:
Word Objects
diw
- delete inner word (word only)daw
- delete a word (including surrounding spaces)
Original: Hello world example
Cursor anywhere on "world" → press diw
Result: Hello example
Cursor anywhere on "world" → press daw
Result: Hello example
Parentheses, Brackets, and Quotes
di(
ordi)
- delete inside parenthesesda(
orda)
- delete around parentheses (including the parentheses)di[
ordi]
- delete inside square bracketsdi"
- delete inside double quotes
Original: function add(x, y) { return x + y; }
Cursor inside parentheses → press di(
Result: function add() { return x + y; }
Cursor inside parentheses → press da(
Result: function add { return x + y; }
Sentence and Paragraph Objects
dis
- delete inner sentencedas
- delete around sentencedip
- delete inner paragraphdap
- delete around paragraph
Visual Mode Deletion
Sometimes it’s easier to select text visually before deleting:
- Press
v
to enter Visual mode - Use arrow keys or motion commands to select text
- Press
d
to delete the selection
Original: The quick brown fox jumps
1. Place cursor on "quick"
2. Press v (enter Visual mode)
3. Press 2w (select "quick brown ")
4. Press d (delete selection)
Result: The fox jumps
Cut, Copy, and Paste Integration
An important concept: in Neovim, “delete” operations actually cut text to a register (clipboard). This means you can paste deleted text elsewhere using p
(paste after cursor) or P
(paste before cursor).
Original: Hello World
1. Place cursor on "World"
2. Press dw (delete word)
3. Move cursor after "Hello"
4. Press p (paste)
Result: HelloWorld
Practical Examples
Cleaning up Code
Remove empty lines:
Position cursor on empty line and press dd
.
Delete trailing semicolon:
Original: let x = 5;
Move cursor to ';' → press x
Result: let x = 5
Delete function parameters:
Original: function calculate(a, b, c) {
Cursor inside parentheses → press di(
Result: function calculate() {
Refactoring Text
Change a quoted string:
Original: const message = "Hello World";
Cursor inside quotes → press di"
Result: const message = "";
Delete until a specific character:
Use dt
+ character to delete until (but not including) that character:
Original: [email protected]
Cursor at beginning → press dt@
Result: @domain.com
Common Mistakes and Tips
Remember the mode: Most delete commands only work in Normal mode. Press
Esc
if you’re unsure.Start simple: Begin with
x
,dd
, anddw
before moving to complex text objects.Use counts: Remember you can prefix most commands with numbers (
3dd
,5dw
, etc.).Undo is your friend: Press
u
to undo any deletion. PressCtrl+r
to redo.Practice with purpose: Don’t just memorize commands; use them while editing real files.
Quick Reference
Command | Action |
---|---|
x | Delete character under cursor |
X | Delete character before cursor |
dd | Delete entire line |
dw | Delete to beginning of next word |
d$ | Delete to end of line |
d0 | Delete to beginning of line |
diw | Delete inner word |
di( | Delete inside parentheses |
di" | Delete inside quotes |
3dd | Delete 3 lines |
dG | Delete from cursor to end of file |
Next Steps
Once you’re comfortable with these deletion commands, explore:
- Change commands (
c
instead ofd
) which delete and enter Insert mode - Search and replace (
:s/old/new/g
) - Macros for repeating complex deletion patterns
- Plugins that extend Neovim’s text manipulation capabilities
Remember, mastering Neovim’s delete functionality is about building muscle memory. Start with the basics and gradually incorporate more advanced commands into your workflow. With practice, you’ll find yourself editing text much faster than you ever thought possible!