How can I select an HTML tag's content in Vim?

43

10

Is it possible to select an HTML tag's content in Vim?

For example:

<p>I am a silly butterfly</p>

I would like to select "I am a silly butterfly".

I know that you can use vi" to select text inside quotes, brackets, etc. The problem here is that the text inside the HTML tags is not really within anything that I can use the i function for.

fholgado

Posted 2010-08-30T01:58:47.283

Reputation: 533

Answers

68

You can use vit to visually select text in a tag. See

:help v_it

garyjohn

Posted 2010-08-30T01:58:47.283

Reputation: 29 085

1Am I the only one whose vim selects the opening tag with vit? It's been bothering me for years and have no idea why Vim does that. – Jonathan Dumaine – 2015-05-03T05:09:46.777

2Note that you can also use both multiple times, eg. vatat – user1338062 – 2015-07-25T16:23:40.500

2Perfect! Gonna write a post about this in my blog, I couldn't find anything on Google with "Vim selecting html tag contents" and others. – fholgado – 2010-08-30T02:48:43.617

You can also substitute v with many other commands, I often use c to change; > and < to fix indentation. – dukedave – 2012-04-19T17:24:32.570

How do you select the tags as well? In this case the <p> tags. – hobbes3 – 2013-04-26T18:35:22.673

8@hobbes3: To include the tags in the selection, use vat. I think of it as in a tag and at as a tag. See :help at. – garyjohn – 2013-04-26T19:07:33.850

6

Here are the steps:

  1. Place the cursor on the tag.
  2. Enter Visual mode by pressing v.
  3. Select the inner tag block by pressing i+t (or a+t for outer tag block).

at a <tag> </tag> block (with tags)

it inner <tag> </tag> block

See more at "How to jump between matching HTML/XML tags?".

kenorb

Posted 2010-08-30T01:58:47.283

Reputation: 16 795

1

If the format is well indent like this,

<div>
  <p>
    I am a silly butterfly
  </p>
  <p>
    I am a silly butterfly
  </p>
</div>

You use can vim-indent-object plugin, and type vii

foxiris

Posted 2010-08-30T01:58:47.283

Reputation: 111

This is exactly what I've been looking for. Thanks! – Ben Davis – 2020-02-17T16:49:02.253