2.2 β€” Word-Based Movement

2.2 β€” Word-Based Movement#

Moving character by character is precise but slow. Word-based movement lets you jump through text much faster while maintaining precision.

Words vs WORDs#

Vim has two definitions of a β€œword”:

word (lowercase)#

A sequence of letters, digits, and underscores, OR a sequence of other non-blank characters.

Examples of separate words:

hello         β†’  1 word
hello_world   β†’  1 word
hello-world   β†’  3 words (hello, -, world)
hello.world   β†’  3 words (hello, ., world)
$variable     β†’  2 words ($, variable)

WORD (uppercase)#

A sequence of non-blank characters separated by whitespace.

Examples of separate WORDs:

hello         β†’  1 WORD
hello_world   β†’  1 WORD
hello-world   β†’  1 WORD
hello.world   β†’  1 WORD
$variable     β†’  1 WORD

Think of it this way: words respect punctuation; WORDs only care about spaces.

Word Movement Commands#

Forward Movement#

CommandAction
wMove to start of next word
WMove to start of next WORD
eMove to end of current/next word
EMove to end of current/next WORD

Backward Movement#

CommandAction
bMove to start of current/previous word
BMove to start of current/previous WORD
geMove to end of previous word
gEMove to end of previous WORD

Visualizing the Difference#

Consider this line:

user.firstName = "John";

Using w (word):

user.firstName = "John";
^    ^         ^ ^     ^
w    w         w w     w

Stops at: user, firstName, =, "John", ;

Using W (WORD):

user.firstName = "John";
^              ^ ^
W              W W

Stops at: user.firstName, =, "John";

Using Counts#

All word motions accept counts:

CommandAction
3wMove forward 3 words
2bMove back 2 words
4WMove forward 4 WORDs
2eMove to end of 2nd word ahead

Word Motions with Operators#

Word motions combine powerfully with operators:

Deleting Words#

CommandAction
dwDelete from cursor to start of next word
deDelete from cursor to end of word
dbDelete from cursor to start of word
diwDelete entire word (cursor anywhere in word)
dawDelete word plus surrounding space

Changing Words#

CommandAction
cwChange from cursor to end of word
ceChange from cursor to end of word (same as cw)
ciwChange entire word
cawChange word plus surrounding space
cWChange from cursor to end of WORD

Yanking Words#

CommandAction
ywYank from cursor to start of next word
yiwYank entire word
yawYank word plus surrounding space

The iw vs aw Distinction#

When using text objects (covered in Chapter 4), there’s an important difference:

Example: In hello world goodbye

       ↓ cursor
hello world goodbye
      ^^^^^         iw selects "world"
      ^^^^^^        aw selects "world " (including trailing space)

This matters when deleting:

Practical Examples#

Example 1: Navigating Code#

const calculateTotal = (items, taxRate) => {

From the start of the line:

Example 2: Editing a Variable Name#

You want to change oldName to newName:

const oldName = "value";
      ^cursor

Example 3: Deleting a Method Call#

object.oldMethod().result
       ^cursor on 'o' of oldMethod

When to Use word vs WORD#

Use w, e, b (word) when:#

Use W, E, B (WORD) when:#

Practice Exercises#

Exercise 1: Basic Word Navigation#

  1. Open a file with prose text
  2. Press w repeatedly to move forward word by word
  3. Press b repeatedly to move backward
  4. Press e to move to word ends

Exercise 2: word vs WORD#

  1. Create a line: user.name = getData().value;
  2. Go to start of line with 0
  3. Press w repeatedly, note where it stops
  4. Go back with 0
  5. Press W repeatedly, compare the stops

Exercise 3: Counts with Words#

  1. Open any text file
  2. Press 5w to move forward 5 words
  3. Press 3b to move back 3 words
  4. Press 2e to move to 2nd word end ahead

Exercise 4: Deleting Words#

  1. Create line: delete this word here
  2. Position on β€œthis”
  3. Press dw β€” what happens?
  4. Undo with u
  5. Press diw β€” compare
  6. Undo with u
  7. Press daw β€” compare

Exercise 5: Changing Words#

  1. Create line: oldValue = calculate(oldInput)
  2. Navigate to oldValue
  3. Press ciw, type newValue, press Esc
  4. Navigate to oldInput
  5. Press ciw, type newInput, press Esc

Common Mistakes#

Using dw When You Mean diw#

dw deletes from cursor to next word, which may leave partial words if cursor isn’t at word start.

Fix: Use diw to delete the entire word regardless of cursor position.

Forgetting ge#

Many Vim users know w, e, b but forget ge (end of previous word).

Use case: You’re at the start of a word and want to go to the end of the previous word.

Over-using Character Movement#

If you’re pressing l many times to get to a word, you should use w, e, or b instead.

Summary#

Motionword (lowercase)WORD (uppercase)
Forward to startwW
Forward to endeE
Backward to startbB
Backward to endgegE
With OperatorsCommand
Delete worddw, diw, daw
Change wordcw, ciw, caw
Yank wordyw, yiw, yaw

Word-based navigation is a major upgrade from character movement. Master it, and you’ll move through text significantly faster.