How do I change the content between HTML tags without manually selecting it?
Answer
cit
Explanation
The cit command deletes everything between the nearest pair of HTML/XML tags and drops you into insert mode, ready to type the replacement. Vim's tag text objects understand nested tags and matching pairs, making this indispensable for editing markup languages.
How it works
cis the change operator (delete and enter insert mode)itis the "inner tag" text object — it selects the content between the opening and closing tags, excluding the tags themselves- Vim finds the nearest enclosing tag pair around the cursor and operates on the content between them
atis the "a tag" variant — it selects the content and the tags themselves
Example
Given the HTML with the cursor anywhere between the <h1> tags:
<h1>Old Title</h1>
Pressing cit removes Old Title and enters insert mode:
<h1>|</h1>
Type New Title and press <Esc>:
<h1>New Title</h1>
This also works with nested tags. Given:
<div>
<p>Some paragraph text here</p>
</div>
With the cursor on paragraph, pressing cit changes the content inside the <p> tags (the innermost enclosing pair). To change the content inside the <div> instead, first press <Esc>, then use 2cit or position the cursor outside the <p> tags.
Tips
- Use
ditto delete the tag content without entering insert mode - Use
yitto yank (copy) the content between tags - Use
vitto visually select the content first so you can verify the boundaries datdeletes the entire element including the opening and closing tags — perfect for removing an element completelycatchanges the entire element including tags, letting you replace both the tags and their content- These text objects work with self-closing tags, XML, JSX, Vue templates, and any well-formed markup
- Tag text objects handle multi-line content seamlessly —
citon a<div>that spans 20 lines selects all 20 lines of content - Combine with
.to repeat: aftercit, type your replacement, press<Esc>, navigate to another tag, and press.to perform the same replacement