What is the difference between the inner word (iw) and inner WORD (iW) text objects in Vim?
Answer
iW vs iw
Explanation
Vim has two flavors of the "inner word" text object that are easy to confuse: iw (lowercase) and iW (uppercase). Knowing when to use each saves time when working with compound identifiers, URLs, file paths, and hyphenated names.
How it works
iw— selects the current word, defined as a sequence of letters, digits, and underscores (\wcharacters). It stops at punctuation like.,-,/,:.iW— selects the current WORD, defined as any sequence of non-whitespace characters. It treats.,-,/,:, and other symbols as part of the WORD.
The same distinction applies to aw / aW ("a word"/"a WORD", which include surrounding whitespace).
Example
Given the cursor positioned anywhere on foo.bar:
const url = 'https://example.com/path/file.js';
ciwwith cursor onexamplechanges onlyexampleciWwith cursor onexample.comchanges the entireexample.com
With cursor on file.js:
const url = 'https://example.com/path/';
diwdeletes justfile, leaving.jsbehinddiWdeletesfile.jsentirely
Tips
- Use
ciWto replace an entire URL, path, or hyphenated CSS class name in one keystroke viWquickly selects asnake_case,kebab-case, orpath/namefor further operations- Combine with
yto yank:yiWcopies the full non-whitespace token (e.g.,[email protected]) - The same uppercase/lowercase distinction applies to motion commands:
wvsW,bvsB,evsE