Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I duplicate an entire entry in an XML file with VIM?

Tags:

vim

xml

I have the following in an XML file:

<TypeDef name="a">
  <ArrayType high="14" low="0">
    <UndefType type="node">
    </UndefType>
  </ArrayType>
</TypeDef>

And I want to copy this entry. Normally I'd go to the first line, enter Line-Visual mode using V, go down to the last line, yank and paste.

How can I copy the entire entry without looking for the end of the entry myself?

like image 464
Nathan Fellman Avatar asked Feb 18 '09 09:02

Nathan Fellman


1 Answers

The solution I learned today (thanks to CMS who answered this question) is to use VIM's text object motions.

Put the cursor in the entry to copy, and type the following in command mode: yat:

  • y yanks according to the following movement.
  • at selects the current tag.

Note that if the cursor is inside the "ArrayType" tag, then that's what will be yanked.
Also note that this won't yank the entire lines. Only from the opening brace of the opening tag to the closing brace of the closing tag. This may cause alignment issues if you're not careful.

One way to get around this is by pasting with :put instead of just p, like this: yat:put.
Note that this won't preserve indentation, because the XML entry wasn't yanked as a whole line.

Another way to do it is: vatVy:

  • v enters Visual Mode.
  • at is as above.
  • V switches to Line Visual mode and selects the entire line.
  • y yanks the selection.
like image 74
Nathan Fellman Avatar answered Oct 20 '22 01:10

Nathan Fellman