C.W.K.
Stream
Lesson 02 of 06 · published

Text Objects — Operating on Structure

~14 min · vim, text-objects, structure

Level 0Trapped
0 XP0/35 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

From characters and words to structure

Motions like w or $ are directional — they go somewhere from the cursor. Text objects are structural — they describe a region defined by what's around it. "Inside the parentheses," "around this word," "inside the HTML tag." The cursor can be anywhere within the region; Vim figures out the bounds for you.

The two prefixes

  • i = inner — just the content, no delimiters.
  • a = around ("a" as in "a thing") — the content plus delimiters or surrounding whitespace.

So i" is "inside the double quotes," a" is "the double-quoted thing including the quotes."

The text objects you'll use a hundred times a day

Words and WORDs: iw / aw (treats punctuation as a boundary), iW / aW (only whitespace as boundary).

Quotes: i", a", i', a', i`, a`.

Brackets: i( = i) = ib (inside parens), i{ = i} = iB (inside braces), i[ = i], i< = i>. All have a variants that include the brackets themselves.

HTML / XML tags: it (inside tag — the content between <p> and </p>), at (around tag — including the tags).

Sentences and paragraphs: is / as, ip / ap.

Cursor position doesn't matter. ci" works the same whether your cursor is on the first character of the string, the last, or anywhere between. Vim scans outward to find the enclosing delimiters. This is why text objects are faster than visual selection: you don't position-then-select, you just name the region.

Power combos you should burn into muscle memory

  • ci" — change inside double quotes ("rename a string").
  • ci( — change inside parentheses ("rewrite the function args").
  • ci{ — change inside braces ("rewrite this block").
  • cit — change inside HTML tag content.
  • diw — delete this whole word.
  • daw — delete this word and the trailing whitespace (so the rest of the line stays clean).
  • dap — delete the entire paragraph.
  • vi{ — visually select inside braces (great for indenting blocks: vi{>).

Code

The full text-object table·vim
" Words
iw  aw       " inner / around word (punctuation = boundary)
iW  aW       " inner / around WORD (whitespace = boundary)

" Quotes
i"  a"   " inside / around double quotes
i'  a'       " inside / around single quotes
i`  a`       " inside / around backticks

" Brackets (each has a synonym for the closing bracket)
i(  i)  ib   " inside parentheses
a(  a)  ab   " around parentheses (including them)
i{  i}  iB   " inside braces
a{  a}  aB   " around braces
i[  i]       " inside square brackets
i<  i>       " inside angle brackets

" HTML / XML tags
it  at       " inside / around tag

" Sentences and paragraphs
is  as       " inner / a sentence
ip  ap       " inner / a paragraph
Power combos — these run the world·vim
ci"     " change inside double quotes
ci(     " change inside parentheses
ci{     " change inside braces
cit     " change inside HTML tag content
diw     " delete inner word (cursor stays where the word was)
daw     " delete a word + surrounding space (line stays tidy)
dap     " delete a paragraph + the blank line after it
yi{     " yank inside braces
vap     " visually select a paragraph
vi(     " visually select inside parens (great for >, =, gq)

External links

Exercise

Open a file with at least one function definition. Without using arrow keys: (1) change a string inside double quotes anywhere on the screen with ci"; (2) replace the parameters of a function call with ci(; (3) delete an entire paragraph with dap; (4) reformat the body of a function with =i{. Each one should be a single composed verb.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.