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

How do I select an HTML tag and its contents in Vim?

Answer

vat

Explanation

The vat command visually selects the nearest enclosing HTML or XML tag and all of its contents, including the opening and closing tags themselves. It combines v (visual mode) with the at ("a tag") text object.

How it works

  • v enters visual mode
  • at is the "a tag" text object — it selects the opening tag, the closing tag, and everything in between

Example

Given the text with the cursor anywhere inside the <div> tag:

<div class="container">
    <p>Hello, world!</p>
</div>

Pressing vat selects the entire block — from <div class="container"> through </div>, including the tags themselves and all inner content.

Compared to vit

  • vit selects the inner tag — only the content between the opening and closing tags, excluding the tags themselves
  • vat selects around the tag — the content plus both the opening and closing tags

For the HTML <p>Hello, world!</p>:

  • vit selects Hello, world!
  • vat selects <p>Hello, world!</p>

Tips

  • Use dat to delete the tag and its contents in one stroke
  • Use cat to change (replace) the entire tag and its contents
  • Use yat to yank the full tag with its contents
  • Pressing at multiple times in visual mode expands the selection to the next enclosing tag — press vatatat to select increasingly outer tags
  • Works with self-closing tags and nested tags — Vim finds the nearest matching pair
  • Combine with dit to delete only the content while preserving the tags themselves

Next

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