8

Given a common html bit like

<div>
  <p>
    Mary had a <b>little</b> lamb.
  </p>
</div>

I want to cut the entire (not the inner only) div for pasting elsewhere. I know there are alternative ways to do this like cutting a range of lines etc., but as a newish user of VIM I sorely need to move around tags in my frontend workflow and I just haven't seen a great way to do this yet.

iamnotsam
  • 9,470
  • 7
  • 33
  • 30
  • @JonathanLandrum I'm guessing it's because he has other tags/text on the same line before the div. – ElGavilan May 16 '14 at 15:07
  • Found a useful answer about yanking and pasting here: http://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim/1220118#1220118 – Jonathan E. Landrum May 16 '14 at 15:14
  • @JonathanLandrum sometimes the tag is really large and I cannot see what line number it actually ends on. Secondarily I don't enjoy counting likes beyond like 7 or 8. Maybe I'll develop a better estimation sense as time goes on, but right now I really need to be moving around tags like so many building blocks without mentally switching into counting. If that makes sense. – iamnotsam May 16 '14 at 15:21

4 Answers4

27

With your cusor on the outer div tag: dat

d elete a round t ag

Then paste it with p where you need it.

Tom
  • 15,798
  • 4
  • 37
  • 48
  • This worked for me, thanks! I see there are many ways to do it including going into visual mode (which I try to avoid when possible) but this is very direct for my use case. – iamnotsam May 16 '14 at 15:39
5

Use the at text-object to act on a whole tag and the it text-object to act on the content of a tag.

To act on the current text-object, your command should look like this:

operator + text-object

You can add a count before the text-object to act on count levels of surrounding text-objects:

operator + count + text-object

So, with the cursor on line 1 and 5, you can use the following command to yank the whole <div>:

yat

With the cursor on line 2, 3 or 4, the command becomes:

y2at

With the cursor inside the <b>, the command becomes:

y3at

That said, I suggest you use visual mode instead of counting: it's more intuitive, safer and potentially faster. Just repeat the text-object to expand your selection:

vatat

then:

y

See :help motion.txt.

romainl
  • 186,200
  • 21
  • 280
  • 313
3

vatyp should do that.

vat to visually select around tag the cursor is in

y to yank it

p to paste. Go to the position where you want to paste and use p to paste.

Amit
  • 19,780
  • 6
  • 46
  • 54
1

xml plugin http://github.com/othree/xml.vim is very handy to edit xml.

with this plugin, when your cursor on <div>, press <leader>D, will remove the <div> till </div> inclusive.

and if you press <leader>d, will only remove the <div> and </div> tag, change your text into:

  <p>
    Mary had a <b>little</b> lamb.
  </p>

there are more functionalities, you may want to check. without the xml plugin, you can press dat

Kent
  • 189,393
  • 32
  • 233
  • 301