How to use text objects in Vim
iwi"i(apA text object names a piece of text by its shape: a word, the inside of quotes, the inside of brackets, a paragraph. Type an operator, then i for the inside or a for the piece with its edges, then the shape.
So ci" changes what is inside the quotes, da( deletes the brackets and what they hold, and yap copies a paragraph. The cursor can be anywhere inside the piece.
Worked example
Replace the message but keep its quotes. The cursor is on not.
i" is the text between the quotes, spaces included. a" would take the quotes as well.
Commands
- iwaw
- A word; aw adds the space next to it.
- iWaW
- A WORD: everything between spaces, punctuation included.
- i"a"
- The inside of double quotes, or the quotes too.
- i'a'
- The same for single quotes.
- i(a(
- The inside of round brackets, or the brackets too. ib and ab mean the same.
- i[a[
- The inside of square brackets.
- i{a{
- The inside of curly brackets. iB and aB mean the same.
- itat
- The inside of an HTML element, or the element with its tags.
- ipap
- A paragraph: lines between blank lines. ap adds the blank line after it.
Inner or around?
The letter after the operator decides how much goes. i keeps the edges: the quotes, the brackets, the tags. a takes them too. Deleting a paragraph shows the difference well.
- dip
- Delete the paragraph and keep the blank line after it.
- dap
- Delete the paragraph and the blank line after it.
- ci(
- Keep the brackets and change what they hold.
Exceptions
- With nested brackets, the innermost pair around the cursor counts.
- Brackets can span several lines, and di( deletes every line inside. Vim leaves the closing bracket on its own line; this site's editor puts it straight after the opening one.
- Start inside the piece you mean. With the cursor before an opening bracket, Vim 9.1 uses the next pair on the line, but this site's editor does nothing.
- Quotes stay on one line: if the closing quote is on the next line, di" does nothing.
- it and at need a matching opening and closing tag around the cursor. Without one, nothing changes.