Text objects make edits like 'change inside quotes' one fluent command that also repeats cleanly with dot.
After an operator (or in Visual mode) you can use a text object instead of a motion. A text object selects a whole thing around the cursor, regardless of where in it you are. They come in two flavours:
i= inner: just the contents.a= a / around: the contents plus the surrounding delimiters or whitespace.
The common objects:
iw/awa word,iW/aWa WORDis/asa sentence,ip/apa paragraphi"i'`i`` quoted stringsi((orib),i[,i{(oriB),i<bracketsit/atan XML/HTML tag's contents or whole element
So with the cursor anywhere inside "hello world", ci" deletes hello world and drops you into Insert mode between the quotes. da( deletes a parenthesised expression including the parentheses; yi{ yanks a function body; dap deletes a paragraph and the blank line after it.
For quotes, Vim even searches forward on the line: ci" works when the cursor is before the first quote. Because a text object does not depend on cursor position, edits made with them are perfect for the dot command: ciwnew<Esc> on one word, then * and . on the next.
Example
" cursor anywhere inside: print("hello world")
ci" " -> print("|") change the string contents
da( " -> print delete the parens and contents
yi( " yank "hello world" with its quotes
diw " delete the word under the cursor
cit " <b>bold</b> -> <b>|</b> change tag contents
dap " delete the paragraph plus trailing blank line
vi{ " visually select a block's contentsRemember
- i = inside, a = around (includes delimiters/whitespace)
- w, s, p, quotes, brackets and t (tags) are all objects
- Text-object edits repeat well with .
Watch out for
- Using cw in the middle of a word only changes from the cursor onward; ciw changes the whole word.