Chapter 8: Text Objects
So far, when you delete, modify, or copy, the target has been either a character (x), a whole line (dd), or a range you carved out with motion commands (d$, dw). But at work what we usually think is "change the string inside these quotes" or "delete the content inside this pair of brackets", not "let me count how many characters there are".
This is where one of Vim's most delightful features comes in — Text Objects.
What is a text object
A text object is not a command, but a kind of "target". It must follow an operator (d delete, c change, y yank, v select) to describe "which structured chunk of text to operate on".
Text objects are always two letters. The first letter is the scope modifier:
imeans inner — only the content inside the objectameans around — including the surrounding symbols/whitespace
The second letter is the object type (word, quotes, brackets, etc., see below).
Note: A text object does not depend on where exactly the cursor sits within the object — it just needs to be somewhere inside the object. For example, with the cursor anywhere between the quotes, ci" changes the whole quoted content.
Common text objects
Word
iwa word (excluding surrounding whitespace)awa word (including whitespace on one side)
Quotes
i"i'i`the content inside the quotesa"a'a`including the quotes themselves
Brackets
i(oribinside parenthesesi[inside square bracketsi{oriBinside curly bracesa(a[a{including the brackets themselves
Tags (HTML/XML)
itthe content inside a tag pair, e.g. between<a>and</a>atincluding the tags themselves
Paragraph and sentence
ip/apa paragraphis/asa sentence
Combining with operators
The real power of text objects comes from combining them with operators — it reads almost like a sentence:
ciwchange inner word — change the current worddi"delete inner quotes — delete the content inside the quotesya(yank around parens — copy the whole chunk including parenthesesvi{visually select inner braces — select the content inside curly bracesdapdelete a paragraph — delete the whole paragraphcitchange inner tag — change the text inside a tag
Tips: The mnemonic is simple — "verb + i/a + object". For example, "I want to change (c) the inner (i) word (w)" is ciw.
Practice with the code below. Move the cursor to different positions and try
ci"to change a string,ci(to change arguments,di{to empty a function body,cit(inside a tag), and so on. Made a mistake? Justuto undo.
const greeting = "hello world";
const user = { name: "seven", age: 18, tags: ["vim", "editor"] };
function render(title, content) {
return `<div class="card"><h1>${title}</h1><p>${content}</p></div>`;
}Try these combinations and feel the difference:
- With the cursor on
hello world, how doci"andca"differ?- With the cursor inside
{ name... }, what aboutdi{versusda{?- With the cursor inside
["vim", "editor"], what doesvi[select?
Once you master text objects, you'll find yourself pressing fewer keys yet editing more precisely. This is the watershed between "knowing Vim" and "enjoying Vim".
Next Chapter introduces Vim's registers and clipboard.