Editing Text
Summary: in this tutorial, you will learn learn vim's powerful operator + motion formula for editing text efficiently using delete, change, yank, put, and the dot command.
Editing Text
Now that you know how to move around in Vim, it's time to learn how to actually edit text. This is where Vim's design really starts to shine — instead of reaching for your mouse to select and delete text, you combine simple building blocks into powerful editing commands.
What you'll learn in this tutorial:
- Vim's secret formula: operator + motion (the key to everything)
- How to delete, change, copy, and paste text
- What "text objects" are and how they save you time
- The magical dot command (
.) that repeats your last edit - How to undo and redo changes
- How Vim's clipboard ("registers") works
The Operator + Motion Formula
This is the single most important concept in Vim editing. Almost every edit in Vim follows this pattern:
operator + motion = action
- An operator is what you want to do (delete, change, copy, etc.)
- A motion is where you want to do it (a word, a line, to the end of the line, etc.)
You already know motions from the previous tutorial (w, b, $, gg, etc.). Now you'll combine them with operators.
Here are the three most important operators:
| Operator | What It Does | Memory Trick |
|---|---|---|
d | Delete (and save what was deleted) | "delete" |
c | Change (delete, then put you in Insert mode to type replacement) | "change" |
y | Yank (copy — does NOT delete anything) | "yank" (Vim's word for copy) |
Combining Operators with Motions
Let's see the formula in action. Remember, w moves to the next word and $ moves to the end of the line:
dw " Delete from cursor to the start of the next word
d$ " Delete from cursor to the end of the line
d0 " Delete from cursor to the beginning of the line
dgg " Delete from cursor to the top of the file
dG " Delete from cursor to the bottom of the file
cw " Change (delete + enter Insert mode) to the next word
c$ " Change from cursor to the end of the line
c0 " Change from cursor to the beginning of the line
yw " Yank (copy) from cursor to the next word
y$ " Yank from cursor to the end of the line
ygg " Yank from cursor to the top of the fileSee the pattern? Once you learn one operator, you can pair it with ANY motion. If you know 3 operators and 10 motions, that's 30 different commands — all from a simple formula. This is what makes Vim so efficient.
Using Counts
You can also add numbers. Remember 3w moves forward 3 words? The same works with operators:
d3w " Delete the next 3 words
c2w " Change the next 2 words
y5j " Yank (copy) 5 lines downward
d2} " Delete through the next 2 paragraphsThe full formula is: [count] + operator + [count] + motion
Operating on a Whole Line
If you press an operator key twice, it acts on the entire current line:
dd " Delete the entire current line
cc " Change the entire current line (deletes it, enters Insert mode)
yy " Yank (copy) the entire current lineYou can add counts here too:
3dd " Delete 3 lines (current line and 2 below)
5yy " Yank (copy) 5 lines
2cc " Change 2 linesDeleting Text
Here are the most common ways to delete text:
| Command | What It Deletes | When to Use It |
|---|---|---|
x | The single character under the cursor | Quick fix — delete one letter |
X | The character before the cursor (like Backspace) | Delete the character you just passed |
dw | From cursor to the next word | Remove one word |
db | From cursor backward to the start of the previous word | Remove the word behind you |
dd | The entire current line | Delete a whole line |
D | From cursor to the end of the line | Same as d$ — clear the rest of a line |
d0 | From cursor to the beginning of the line | Clear everything before the cursor |
dG | From cursor to the end of the file | Delete everything below |
dgg | From cursor to the top of the file | Delete everything above |
Important: When you delete text in Vim, it's not really "deleted" — it's actually saved in a temporary clipboard. You can paste it somewhere else with p. Think of d as "cut", not "delete". This is covered more in the "Putting (Pasting) Text" section below.
Changing Text
The c (change) operator is like d (delete) but it also puts you into Insert mode so you can type replacement text right away. It's essentially "delete this, then let me type something new."
| Command | What It Does |
|---|---|
cw | Delete to the end of the word, then start typing |
cb | Delete backward to the start of the word, then start typing |
cc | Delete the whole line, then start typing (keeps the indentation!) |
C | Delete from cursor to end of line, then start typing (same as c$) |
s | Delete the character under the cursor, then start typing |
S | Delete the entire line, then start typing (same as cc) |
Here's a practical example. Say you have this line and your cursor is on quick:
The quick brown fox
If you press cw, the word quick disappears and you're now in Insert mode. You can type slow and then press Escape to get:
The slow brown fox
Compare that to pressing dw (which deletes quick but stays in Normal mode) — you'd then need to press i to enter Insert mode and type slow. The c operator saves a step.
Yanking (Copying) Text
Yank is Vim's word for "copy." It copies text without removing it.
| Command | What It Copies |
|---|---|
yy | The entire current line |
yw | From cursor to the start of the next word |
y$ | From cursor to the end of the line |
Y | The entire current line (same as yy) |
yG | From cursor to the end of the file |
ygg | From cursor to the top of the file |
Yanked text goes into the clipboard (Vim calls it a "register"). You can then paste it with p or P.
Putting (Pasting) Text
After you've deleted (d) or yanked (y) text, use p or P to paste it:
| Command | Where It Pastes |
|---|---|
p | After the cursor (or below the current line if you copied a whole line) |
P | Before the cursor (or above the current line if you copied a whole line) |
Here's a quick workflow to move a line:
dd " Delete (cut) the current line
" Now move your cursor to where you want the line to go
p " Paste the line below your current positionAnd here's how to duplicate a line:
yy " Copy the current line
p " Paste it below (you now have two copies of the line)Quick swap trick: Want to swap two lines? Put your cursor on the first line, press ddp. This deletes the line (dd), then pastes it back one line below (p), effectively swapping it with the line that was below it.
Text Objects (Selecting Smart Regions)
Text objects are one of Vim's most powerful features. They let you operate on a whole "thing" — like a word, a sentence, a paragraph, or everything inside quotes — regardless of where your cursor is within that thing.
Text objects use two prefixes:
i= inner (the content only, not the surrounding characters)a= a/around (the content plus the surrounding characters)
| Text Object | What It Selects (inner) | What It Selects (around) |
|---|---|---|
iw / aw | The current word | The word plus surrounding spaces |
is / as | The current sentence | Sentence plus trailing space |
ip / ap | The current paragraph | Paragraph plus blank line after it |
i" / a" | Text inside "..." | Text including the quotes themselves |
i' / a' | Text inside '...' | Text including the quotes |
i) or ib / a) or ab | Text inside (...) | Text including the parentheses |
i] / a] | Text inside [...] | Text including the brackets |
i} or iB / a} or aB | Text inside {...} | Text including the braces |
it / at | Text inside an HTML/XML tag | Text including the tags |
You combine these with operators:
diw " Delete the word the cursor is on (no matter where in the word you are)
ciw " Change the word — delete it and enter Insert mode
daw " Delete the word plus the space after it (cleaner deletion)
ci" " Change everything inside double quotes
di) " Delete everything inside parentheses
da] " Delete everything inside brackets, including the brackets
dit " Delete the content inside an HTML tag
yiw " Yank (copy) the current wordWhy text objects are so powerful: Without text objects, to delete a word, you'd need to first move your cursor to the beginning of the word (b), then delete to the end (dw). With diw, your cursor can be anywhere inside the word and it deletes the whole thing. No need to position your cursor first!
Practical Example
Imagine your cursor is somewhere on the word blue in this line:
colors = ["red", "blue", "green"]Here are some text-object commands and what they'd do:
| You Type | Result |
|---|---|
ciw | Deletes blue, enters Insert mode so you can type a replacement |
ci" | Deletes blue (the content inside the quotes), enters Insert mode |
da" | Deletes "blue" (including the quotes) |
di] | Deletes "red", "blue", "green" (everything inside the brackets) |
da] | Deletes ["red", "blue", "green"] (brackets too) |
The Dot Command (Repeat Last Edit)
The dot command (.) is one of Vim's most beloved features. It repeats your last change. Whatever edit you just made — deleting a word, changing some text, inserting something — pressing . does it again.
Here's a practical example. Say you want to delete several words scattered through a file:
daw " Delete the first word (let's say "unnecessary")
" Move your cursor to the next word you want to delete
. " Pressing dot repeats "daw" — deletes this word too
" Move to another word
. " Delete that one tooAnother example — adding a semicolon to the end of several lines:
A; " Go to end of line and type a semicolon (A enters Insert mode at end)
<Esc> " Back to Normal mode
j " Move down one line
. " Repeat: go to end of line and add semicolon
j. " Move down and repeat again (you can chain j and . together)Design your edits to be repeatable! When you know you'll need to make the same change multiple times, think about using a single command (like cw or daw) that captures the whole change, so . can repeat it efficiently. This is often called "the Vim way."
Undo and Redo
Made a mistake? No problem:
| Command | What It Does |
|---|---|
u | Undo the last change (press it again to undo more) |
Ctrl-r | Redo (undo the undo — put the change back) |
U | Undo ALL changes made to the current line (less commonly used) |
You can undo as many times as you need — Vim keeps a long history of changes. It even remembers your undo history in a tree structure, which means you never truly lose anything.
Each time you enter Insert mode, type something, and return to Normal mode with Escape, that entire sequence counts as one undo step. So if you press u, it undoes everything you typed during that Insert mode session.
Replace a Single Character
Sometimes you just need to fix one character — maybe a typo. Instead of entering Insert mode for that:
| Command | What It Does |
|---|---|
r{char} | Replace the character under the cursor with {char}, staying in Normal mode |
R | Enter Replace mode — every character you type overwrites the existing text |
" If cursor is on the 't' in "teh":
ra " Changes 't' to 'a' making it "aeh" — that's not right!
" Let's try again. If cursor is on the 'e' in "teh":
rh " Changes 'e' to 'h', making "thh" — still not right!
" Actually, for the typo "teh" → "the", you'd use:
" Put cursor on 'e', then: xp (delete 'e', paste it after 'h')
" Or use: ciw then type "the"The r command is most useful for simple one-character fixes. It stays in Normal mode (no need to press Escape afterward).
Joining Lines
Sometimes you want to combine two lines into one:
| Command | What It Does |
|---|---|
J | Join the current line with the next line (adds a space between them) |
gJ | Join lines without adding a space |
3J | Join the current line with the next 2 lines (3 lines total become 1) |
This is handy when you've broken a line in two and want to recombine it, or when reformatting text.
Summary
Here's what you've learned:
- Vim's editing formula: operator + motion = action
- Three key operators:
d(delete/cut),c(change),y(yank/copy) - Double the key for whole lines:
dd,cc,yy - Paste with
p(after) orP(before) - Text objects like
iw(inner word),i"(inner quotes),i)(inner parentheses) - The dot command (
.) repeats your last change — design edits to be repeatable uto undo,Ctrl-rto redorto replace a single character,Jto join lines- Counts work with operators:
d3wdeletes 3 words
In the next tutorial, you'll learn how to search for text patterns and replace them across your file.
Practice: Editing Drills
Create a practice file and try these exercises:
vim practice.txtPress i to enter Insert mode, type the following text, then press Escape:
The quick brown fox jumps over the lazy dog.
colors = ["red", "blue", "green"]
function calculateTotal(items, tax) {
return items.reduce((sum, item) => sum + item.price, 0) * (1 + tax);
}
This line has a tyypo in it.
This sentence is fine. This one should be deleted. This one is also fine.
Now try these tasks in Normal mode:
- Move to the word
brownand change it toredusingcw - Delete the word
lazyusingdaw - Copy the first line with
yy, move to the end of the file withG, and paste it withp - On the
colorsline, change"blue"to"purple"usingci"(put cursor anywhere on blue first) - Delete everything inside the parentheses of
calculateTotalusingdi( - Fix the typo
tyypo— put cursor on the extrayand pressx - Delete the middle sentence "This one should be deleted." using
das(delete around sentence) - Undo the last change with
u, then redo it withCtrl-r - Use the dot command: delete a word with
daw, move to another word, and press.
Show Solution
" 1. Change "brown" to "red"
" Move cursor to "brown" (use /brown or w to get there)
cw " Deletes "brown", enters Insert mode
red " Type the replacement
<Esc> " Back to Normal mode
" Result: "The quick red fox jumps over the lazy dog."
" 2. Delete the word "lazy"
" Move cursor to "lazy"
daw " Deletes "lazy" and the space around it
" Result: "The quick red fox jumps over the dog."
" 3. Copy first line and paste at end
gg " Go to first line
yy " Yank (copy) the line
G " Go to last line
p " Paste below
" Result: First line is now duplicated at the bottom
" 4. Change "blue" to "purple"
" Navigate to the colors line, put cursor on any letter in "blue"
ci" " Deletes "blue" (inside the quotes), enters Insert mode
purple " Type replacement
<Esc> " Back to Normal mode
" Result: colors = ["red", "purple", "green"]
" 5. Delete inside parentheses
" Navigate to the calculateTotal line, cursor on anything inside ()
di( " Deletes "items, tax" (the content inside parentheses)
" Result: function calculateTotal() {
" 6. Fix the typo
" Navigate to "tyypo", put cursor on the extra 'y'
x " Deletes the single character under the cursor
" Result: "This line has a typo in it."
" 7. Delete a sentence
" Navigate to "This one should be deleted."
das " Deletes the sentence and trailing space
" Result: "This sentence is fine. This one is also fine."
" 8. Undo and redo
u " Undo: the sentence comes back
Ctrl-r " Redo: the sentence is deleted again
" 9. Dot command
daw " Delete any word
" Move cursor to another word
. " Dot repeats daw — that word is deleted too!
:q! " Quit without saving when done practicingWritten by the ShellRAG Team
The ShellRAG editorial team writes practical, beginner-friendly Vim tutorials with tested code examples and real-world use cases. Every article is technically reviewed for accuracy and updated regularly.
Learn more about us →