Vim text objects explained
A text object is a name for a chunk of text around your cursor: a word, a quoted string, the inside of a pair of brackets. Put one after an operator and you can change, delete or copy that chunk without moving the cursor to its edge first. ciw, di( and ci" are the three you will use every day.
The pattern: operator, then i or a, then an object
A text object is always typed after an operator such as d (delete), c (change), y (copy) or gU (uppercase). It has two letters:
- The first letter is
i(inner) ora(around). - The second letter says which object:
wfor word,"for double quotes,(for parentheses, and so on.
So ciw reads "change inner word", and di( reads "delete inner parentheses". The cursor can be anywhere inside the object. If you know the operators, there is nothing new to learn except the list of objects.
Inner vs around
i selects just the contents. a selects the contents plus what wraps them: the quotes or brackets, or for words the space next to them. Use i when you want to replace what is inside and keep the wrapper. Use a when you want the whole thing gone.
Before (cursor on quick) | Keys | After |
|---|---|---|
The quick brown fox | diw | The brown fox (two spaces stay) |
The quick brown fox | daw | The brown fox |
The quick brown fox | ciwslow<Esc> | The slow brown fox |
daw is the one to remember for deleting a word cleanly. ciw is the one to remember for replacing a word from anywhere inside it. The older way, bcw, needs the cursor to be moved to the start of the word first.
Quotes
Quote objects work on the quoted string on the current line. If the cursor is before the first quote, Vim looks ahead along the line, so you do not even have to be inside the string.
| Before | Keys | After |
|---|---|---|
print("Hello, wrold") | ci"Hello, world<Esc> | print("Hello, world") |
greet('bob') | ci'alice<Esc> | greet('alice') |
echo "hello" world | da" (cursor on hello) | echo world (the space after the string goes too) |
Parentheses, braces and brackets
Bracket objects find the nearest matching pair around the cursor, even when the cursor is deep inside. Either bracket works: i( and i) are the same, and so are ib and i(. Braces also have iB.
| Before | Keys | After |
|---|---|---|
sum(a, b, c) | di( | sum() |
sum(a, b, c) | da( | sum |
if (ok) { return 1; } | ci{x<Esc> | if (ok) {x} |
items[index] | ci[0<Esc> | items[0] |
When pairs are nested, as in f(a(b)), the object is the innermost pair that contains the cursor. With the cursor on b, di( gives f(a()).
All the objects the course teaches
| Keys | Selects |
|---|---|
iw aw | the word under the cursor / plus surrounding space |
iW aW | the WORD (everything between spaces) / plus surrounding space |
i" a" | inside double quotes / quotes included |
i' a' | inside single quotes / quotes included |
i( a( | inside parentheses / parentheses included |
i{ a{ | inside braces / braces included |
i[ a[ | inside square brackets / brackets included |
Real Vim has more: paragraphs (ip), sentences (is), HTML tags (it) and angle brackets (i<). Vimrealm does not teach those yet.
Text objects work with every operator
Because a text object is just another way to say how far an operator reaches, everything you know about operators carries over.
| Keys | What it does |
|---|---|
yiw | copy the word under the cursor |
yi( | copy what is inside the parentheses |
gUiw | UPPERCASE the word: say hello world with the cursor on hello becomes say HELLO world |
ci" then . on another line | repeat the change on the next string; see the dot command |
Why they are worth learning early
- Fewer keys, no prep. You skip the "move to the start of the word" step, and the cursor can be anywhere inside the thing.
- They compose. Two or three keystrokes cover an edit that would otherwise take five, and you can read the command back as a sentence.
- They repeat well. A change made with a text object is easy to replay on the next line with
..
Text objects have their own chapter in Vimrealm: the Temple of Objects (chapter 9). You type the real commands, in quests with a par score.
Play Vimrealm freeChapters 1–5 (movement) are free. Text objects are in chapter 9, which is part of the full version ($7.99, one payment). The free chapters teach the motions that text objects build on. See how the course works or keep the cheat sheet entry for text objects open while you practise.