Working Code
Imagine this text is open in Vim:
const userName = "Alice";
const userAge = 25;
const userEmail = "alice@example.com";
With the cursor on the N of userName, try these commands:
dw → delete "ame" (cursor to end of word)
d$ → delete from cursor to end of line
cw → delete to end of word and enter Insert mode
Vim commands read like English sentences. "Delete (d) a word (w)" — a verb (operator) followed by an object (motion).
Try It Yourself
Let's refactor the variable names in this code:
let result = calculateTotal(price, tax);
let message = "Total: " + result;
console.log(message);
- Place the cursor on
resultand presscw.resultis deleted and you enter Insert mode. Typetotaland pressEsc. - On the second line, place the cursor inside
"Total: "and pressci"— that changes only what's inside the quotes (you'll learn this in the next lesson). - On the
console.log(message);line, pressd$to delete to the end of the line.
"Why?"
Vim is a language
Vim commands form a language with grammar rules. Once you learn the rules, you don't memorize commands — you combine them.
Operators (verbs):
| Key | Meaning | Description |
|---|---|---|
d | delete | Delete |
c | change | Delete and enter Insert mode |
y | yank | Copy |
Motions (objects):
| Key | Meaning |
|---|---|
w | To start of next word |
e | To end of current word |
$ | To end of line |
0 | To start of line |
gg | To top of file |
G | To end of file |
Combination table
Combine operators and motions:
w | e | $ | 0 | gg | G | |
|---|---|---|---|---|---|---|
d | dw | de | d$ | d0 | dgg | dG |
c | cw | ce | c$ | c0 | cgg | cG |
y | yw | ye | y$ | y0 | ygg | yG |
Three operators and six motions give you 18 commands.
Numbers make it even more powerful
Prefix the operator or the motion with a number to specify a count:
d2w → delete 2 words
3dd → delete 3 lines starting from the current line
y5j → yank 5 lines downward from the current line
Line-wise shortcuts
Typing an operator twice applies it to the entire current line:
dd → delete the current line
yy → yank the current line
cc → delete the current line and enter Insert mode
The difference between d and c
Both d and c delete text, but c automatically enters Insert mode afterward. Use c when you want to replace text with something else.
dw → delete word, stay in Normal mode
cw → delete word, enter Insert mode → ready to type the new word
Deep Dive
Common shortcut commands
Some operator+motion combos have shorthand keys:
| Shortcut | Original | Meaning |
|---|---|---|
D | d$ | Delete to end of line |
C | c$ | Change to end of line |
Y | yy | Yank the line (note: not y$) |
x | dl | Delete one character |
s | cl | Change one character |
Refactor variables
Open the code below in Vim and edit it using only operator+motion combinations.
var oldName = "hello";
var oldValue = 42;
console.log(oldName + oldValue);
Goals:
- Change every
vartoconst(usecw) - Rename
oldNametogreeting(usecw) - Delete the last line entirely (use
dd)
Hint: Search with /old to jump quickly.
question: "What does d2w do?" options:
question: "What's the difference between cc and dd?" options:
question: "Which command yanks from the cursor to the end of the line?" options: