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:

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)KeysAfter
The quick brown foxdiwThe  brown fox (two spaces stay)
The quick brown foxdawThe brown fox
The quick brown foxciwslow<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.

BeforeKeysAfter
print("Hello, wrold")ci"Hello, world<Esc>print("Hello, world")
greet('bob')ci'alice<Esc>greet('alice')
echo "hello" worldda" (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.

BeforeKeysAfter
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

KeysSelects
iw awthe word under the cursor / plus surrounding space
iW aWthe 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.

KeysWhat it does
yiwcopy the word under the cursor
yi(copy what is inside the parentheses
gUiwUPPERCASE the word: say hello world with the cursor on hello becomes say HELLO world
ci" then . on another linerepeat the change on the next string; see the dot command

Why they are worth learning early

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 free

Chapters 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.