Jump to matching XML tags in Vim

XmlVim

Xml Problem Overview


Vim % operator jumps to matching parentheses, comment ends and a few other things. It doesn't, however, match XML tags (or any other tag, to the best of my knowledge).

What's the best way to jump to the matching XML tag using Vim?

Note: What I really want to do is duplicate a section in an XML file without manually looking for the matching tag.

Xml Solutions


Solution 1 - Xml

You can do this without additional plugins:

  • place cursor on the tag
  • vat - will select the (outer) tag and place cursor on the end
  • once you've got your selection you can toggle between the top and bottom with o (update based on Michael Gruber's note)
  • c - change or, y - copy or, escape for leaving visual mode ...

Another useful operation is: vit - will select content of the tag (inner).

Update (thanks to @elrado) Example: vito will enable you to select inner content of the tag and position cursor on the beginning of the selected text.

Reference: https://superuser.com/questions/182355/how-can-i-select-an-html-tags-content-in-vim

Vim reference (thanks to @Geek for noting this out):

:help visual-operators

you'll get:

4. Operating on the Visual area				*visual-operators*

The objects that can be used are:
	...
	at	a <tag> </tag> block (with tags)		|v_at|
	it	inner <tag> </tag> block			|v_it|
    ...

Solution 2 - Xml

There is a vim plugin called matchit.vim . You can find it here: http://www.vim.org/scripts/script.php?script_id=39 . It was created pretty much the exact purpose you describe.

Install that, place your cursor on the body of the tag (not the <>, else it'll match those) and press % to jump to the other tag. See the script's page to find out what else it matches.

Solution 3 - Xml

The OP stated that what he really wanted to do is copy a section of XML without having to find the matching tag. This is easily done in normal mode with yat<motion>p, which yanks the text inside and including the matching tags, then pastes it. yit<motion>p is almost the same, but it doesn't include the outer tags.

The 'y' in the string is of course the normal mode "yank" command. (:help y)

a or i can be used for object selection after an operator such as y or inside a visual selection. The symbol after a or i specifies what should be selected. The object type t used here indicates an SGML tag. (:help object-select).

Of course <motion> just means to move somewhere by the means of your choice and p puts the yanked text at that location.

Solution 4 - Xml

Just my trick of using "yank", "object-select" (tag select) and "jump to last yanked text".

yit`] 

to jump to right before closing tag

and

yit

to jump to right after opening tag

Note: this will change the content of default register

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan FellmanView Question on Stackoverflow
Solution 1 - XmlRobert LujoView Answer on Stackoverflow
Solution 2 - XmlsykoraView Answer on Stackoverflow
Solution 3 - XmlCodie CodeMonkeyView Answer on Stackoverflow
Solution 4 - XmltransangView Answer on Stackoverflow