Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Editing and Text Objects

Vim Text Objects: iw, i", i(, it and More

Use Vim text objects such as iw, aw, i", a(, it and ip to change or delete structured text from anywhere inside it. Includes a runnable text object finder.

Motions move from the cursor to somewhere else. A text object is different: it names a whole structure around the cursor, such as the word you are in, the quotes surrounding you, or the function arguments in parentheses. Because the cursor can be anywhere inside it, you do not have to move first. Text objects are the feature that makes many people say Vim "gets" code.

Inner and around

Text objects come in pairs: i for inner (the contents) and a for around (the contents plus delimiters or trailing space). They only work after an operator or in Visual mode.

Object Selects
iw / aw a word / a word plus trailing space
iW / aW a WORD / WORD plus space
is / as a sentence
ip / ap a paragraph (ap includes the blank line after)
i" / a" inside double quotes / including the quotes
i' and i` the same for single quotes and backticks
i( i) ib inside parentheses (a( includes them)
i[ i] inside square brackets
i{ i} iB inside braces
i< i> inside angle brackets
it / at inside / including an HTML or XML tag pair

Reading them as English

  • ciw change inner word: fix a mistyped identifier from anywhere in it.
  • di" delete what is inside the quotes, keep the quotes.
  • ci( change a function's argument list.
  • dap delete a whole paragraph and its trailing blank line.
  • yit copy the text inside an HTML tag.
  • vi{ select the body of a block, then > to indent.

ci" also works when the cursor is before the quotes on the same line: Vim searches forward for the next quoted string. That makes ci" more forgiving than most motions.

Inner vs around

i Contents only
a Contents plus delimiters

ci" keeps the quotes; ca" removes them.

1 / 2

Counts and nesting

With nested brackets, a count picks the enclosing level: d2i( deletes inside the second pair of parentheses outward. Repeating the selection in Visual mode does the same: vi( then i( again grows to the next enclosing pair. For nested HTML, dit deletes the content of the innermost tag you are in and d2it the next one out.

iw versus aw

aw takes the word and one adjacent space (trailing space, or leading if there is none). That makes daw the right way to remove a word from a sentence without leaving a double space. ciw is better when you are replacing a word, because you want to keep the spacing.

JS A text object finder (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first i" call has the cursor on p, before the quotes, and still finds the string: that is the look-ahead behavior real Vim also has. The two i( calls show nesting: inside max(...) the cursor selects the inner call, anywhere else it selects the whole argument list.

Tags and prose

it and at understand HTML and XML pairs, <p>text</p>. Put the cursor inside a <li>, type cit, and the contents are replaced while the tags remain. at takes the tags too, so dat removes a whole element. For prose, is and as select sentences, ip and ap paragraphs. das deletes a sentence and leaves the paragraph tidy.

Custom text objects

Plugins add more. vim-textobj-user lets you define your own, and Neovim's Treesitter text objects add af/if for functions, ac/ic for classes and aa/ia for arguments. Chapter 7 covers Treesitter. With those, daf deletes a function from any line inside it. Lua code can also define objects through the onoremap and xnoremap mappings.

Surround without plugins

Vim itself has no "surround" command, but text objects make it manageable. To wrap a word in quotes without a plugin: ciw""<Esc>P replaces the word with an empty pair of quotes, then pastes the old word between them (the word is in the unnamed register). The vim-surround plugin does this in ysiw" and chapter 6 introduces plugins.

JS Daw vs ciw spacing (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does ci" do when the cursor is inside "hello"?

    ci" is change inner quotes: the text between the quotes is replaced, the quotes stay.
  2. 2. Which command removes a word without leaving a double space?

    aw includes one adjacent space, so daw leaves normal spacing.
  3. 3. What does it select?

    it is the tag-block text object; at includes the tags themselves.
  4. 4. How do you select the second enclosing pair of parentheses?

    Both a count and repeating the object in Visual mode extend outward.

Summary

  • i selects the contents; a includes delimiters or surrounding space.
  • Common objects: words, sentences, paragraphs, quotes, brackets, braces, angle brackets and tags (w W s p " ' ( [ { < t).
  • Text objects work from anywhere inside the structure and nest with counts.
  • ciw, di", ci(, dap, cit are everyday editing verbs.
  • Plugins and Treesitter add objects for functions, classes and arguments.

Next: Change, Delete, Replace and Put