Nvim Session 02: WORD Motions & Operators
The difference between w and W seems minor but changes everything. Master WORD motions and the operator + motion grammar.
Pre-Session State
-
Know
h j k lfor movement -
Know
wmoves forward by word -
Understand WORD vs word distinction
-
Understand operator pending mode
The Grammar: Operator + Motion
Vim’s editing language is composable:
{operator}{count}{motion}
or
{count}{operator}{motion}
Operators:
| Operator | Effect |
|---|---|
|
Delete |
|
Change (delete + insert) |
|
Yank (copy) |
|
Indent right |
|
Indent left |
|
Auto-indent |
|
Uppercase |
|
Lowercase |
Motions: w, W, b, B, e, E, $, 0, etc.
Lesson 1: word vs WORD
word: Sequence of letters, digits, underscores (alphanumeric) WORD: Sequence of non-blank characters (whitespace-delimited)
Exercise 1.1: See the difference
Create this test line:
hello-world foo.bar user@host.com /etc/ssh/config
Starting at h in hello:
With w (lowercase):
w → hello|-world (stops at -) w → hello-|world (stops at w) w → hello-world| foo (stops at space) w → hello-world |foo (stops at f)
With W (uppercase):
W → hello-world |foo.bar (skips entire hello-world) W → hello-world foo.bar |user@host.com
Key insight: W treats punctuation as part of the word.
Exercise 1.2: Backward motion
On the same line, cursor at end:
hello-world foo.bar user@host.com /etc/ssh/config|
With b (lowercase):
b → /etc/ssh/|config b → /etc/ssh|/config b → /etc/|ssh/config
With B (uppercase):
B → user@host.com |/etc/ssh/config B → foo.bar |user@host.com
Exercise 1.3: End of word motion
hello-world foo.bar |
With e (lowercase):
e → hell|o-world (end of "hello") e → hello-worl|d (end of "world")
With E (uppercase):
E → hello-worl|d (end of WORD) E → foo.ba|r (end of next WORD)
Lesson 2: Operator + WORD
Concept: Combine operators with WORD motions for powerful edits.
Exercise 2.1: Delete operations
function(arg1, arg2, arg3)
|
dw " Delete to next word boundary → function(, arg2, arg3)
dW " Delete entire WORD → function(arg2, arg3)
Exercise 2.2: Change operations
hostname="old-server.domain.com"
|
cW " Change entire WORD (deletes, enters insert)
new-host.domain.com<Esc>
Result:
hostname="new-host.domain.com"
Lesson 3: Counts with Motions
Concept: Counts multiply the motion.
Exercise 3.1: Count before operator
one two three four five six |
3w " Move forward 3 words → one two three |four
d3w " Delete 3 words
2W " Move forward 2 WORDs
d2W " Delete 2 WORDs
Exercise 3.2: Count before motion
delete these three words please |
d3w " Deletes "delete these three "
3dw " Same result (count can go before operator)
Exercise 3.3: Combining with relative line numbers
With :set relativenumber, you see distances:
3 function setup() 2 local x = 1 1 local y = 2 0 return x + y ← cursor here 1 end 2 3 function teardown()
3k " Jump up 3 lines (to function setup())
2j " Jump down 2 lines (to blank line)
d3k " Delete current line and 3 above
Lesson 4: Operator Pending Mode
Concept: After pressing an operator, Vim waits for a motion.
Exercise 4.1: Understanding the pending state
Press d alone:
d " Vim shows cursor waiting... operator pending
Now complete with motion:
d w " Delete word d $ " Delete to end of line d G " Delete to end of file d gg " Delete to start of file
Press <Esc> to cancel operator pending.
Exercise 4.2: Visual confirmation
With operator pending, Vim often highlights the affected region. Try:
c " Change pending w " Highlights word being changed
Exercise 4.3: Double operator = line
Doubling an operator affects the current line:
dd " Delete line
yy " Yank line
cc " Change line (delete + insert)
>> " Indent line
<< " Outdent line
== " Auto-indent line
gUU " Uppercase line
guu " Lowercase line
Lesson 5: Practical Patterns
Exercise 5.1: Delete to pattern
server: hostname.domain.com:8080 |
dt: " Delete until colon → : hostname.domain.com:8080
d2t: " Delete until 2nd colon → :8080
df: " Delete through colon → hostname.domain.com:8080
Exercise 5.2: Change inside delimiters
config["old-value"]
|
ci" " Change inside quotes
new-value<Esc>
Result:
config["new-value"]
Exercise 5.3: Infrastructure editing
YAML editing:
hostname: old-server.inside.domusdigitalis.dev
|
cW " Change the entire hostname
new-server.inside.domusdigitalis.dev<Esc>
SSH config editing:
Host vault-01
HostName vault-01.inside.domusdigitalis.dev
|
cW " Change entire FQDN in one motion
Summary: WORD Motion Reference
| Motion | Type | Effect |
|---|---|---|
|
word |
Start of next word (stops at punctuation) |
|
WORD |
Start of next WORD (whitespace only) |
|
word |
Start of previous word |
|
WORD |
Start of previous WORD |
|
word |
End of current/next word |
|
WORD |
End of current/next WORD |
|
word |
End of previous word |
|
WORD |
End of previous WORD |
When to Use WORD vs word
| Use WORD (uppercase) | Use word (lowercase) |
|---|---|
URLs, emails, file paths |
Camel/snake case identifiers |
Hyphenated values |
Individual words in prose |
IP addresses |
Editing code tokens |
|
|
Exercises to Complete
-
[ ] Navigate a URL with
Wvsw, feel the difference -
[ ] Delete an email address with
dW -
[ ] Change a file path with
cW -
[ ] Use counts:
d3Wto delete 3 WORDs -
[ ] Practice
dd,yy,ccline operations
Next Session
Session 03: Line & Character Motions - Master 0 ^ $ g_ and f t F T ; ,.
Session Log
| Timestamp | Notes |
|---|---|
Start |
<Record when you started> |
End |
<Record when you finished> |
Discoveries |
<What surprised you?> |