vimtricks.wiki Concise Vim tricks, one at a time.

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

  • c is the change operator (delete and enter insert mode)
  • it is 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
  • at is 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 dit to delete the tag content without entering insert mode
  • Use yit to yank (copy) the content between tags
  • Use vit to visually select the content first so you can verify the boundaries
  • dat deletes the entire element including the opening and closing tags — perfect for removing an element completely
  • cat changes 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 — cit on a <div> that spans 20 lines selects all 20 lines of content
  • Combine with . to repeat: after cit, type your replacement, press <Esc>, navigate to another tag, and press . to perform the same replacement

Next

How do I edit multiple lines at once using multiple cursors in Vim?