tags:

views:

1363

answers:

2

Vim's % 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.

+12  A: 

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.

sykora
Nice one. Has been sitting on my hard disk all the time.
innaM
There's some version of matchit, which ships with Vim already btw. `runtime macros/matchit.vim` should enable it, and allow `%` to match XML tags.
Svend
+5  A: 

No plugins required:

yat - yank an tag

yit - yank inner tags

dat - delete an tag

and so on ...

Oleksandr Bolotov
Would you clarify? I cannot understand how you can jump to the matching tag with "yank" and "delete".
Masi
It doesn't really solve _exactly_ the question answered, but you can do a movement operation on the specified movement. For example, if you want to beautify the contents of a tag, go somewhere inside the content and type gqitand this will only wrap inside of that tag. That kind of thing. This answers the question in that he can go just inside the tag of the _whole block_ and yank it with yatand that will allow him to duplicate a section in his XML file
Matt