45

How to jump between matching tags (such as <div>, <span>, etc.) when editing HTML/XHTML/XML documents similarly as % is used to jump between matching parentheses?

  • 6
    possible duplicate of Using % in languages without curly braces ... This solution also works for HTML (just tested it, if you're on a > it will work like before, if you're on the div it will work like you want). – Martin Tournoij Feb 13 '15 at 22:11
  • @Carpetsmoker If this question would be duplicated of general-like answer (using one plugin), then it won't allow for any answers which are dealing with html tags specifically. – kenorb Feb 14 '15 at 0:02
  • However, the other question has exactly the same answer, so this is a duplicate. – Doorknob Feb 14 '15 at 0:04
  • Posted sample answer which won't fit into other question, so it's not duplicate then. – kenorb Feb 14 '15 at 0:20
  • 1
    @Doorknob The fact that two questions happen to have the same answer does not necessarily mean that the questions are duplicates of each other. – 200_success Feb 14 '15 at 5:49
45

Vim ships with a macro called matchit that does this for you; all you need to do is activate it with runtime macros/matchit.vim in your vimrc. This will enable you to jump from, eg, a <div> to its </div>. Note that your cursor will have to be inside the angle brackets; if you're on the angle brackets, % will jump from one bracket to the other as normal.

23

You can jump between tags using visual operators, in example:

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

Your cursor should jump forward to the matching closing html/xml tag. To jump backwards from closing tag, press o or O to jump to opposite tag.

Now you can either exit visual by pressing Esc, change it by c or copy by y.


To record that action into register, press qq to start recording, perform tag jump as above (including Esc), press q to finish. Then to invoke jump, press @q.


See more help at :help visual-operators or :help v_it:

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

it inner <tag> </tag> block


Alternatively use plugin such as matchit.vim or surround.vim.


See also:

3

The xmledit (http://vimawesome.com/plugin/xmledit) plugin allows to jump between open and close tags using

  • <localleader>%

Often <localleader> will be \, so you can jump with

  • \%

NOTE: Unlike matchit, you don't have to put your cursor inside the tag. Placing it right on the < or > is perfectly fine for the xmledit plugin. :)

NOTE 2: A drawback here is, that you can't use it in the visual mode. But this isn't a big deal, since you can always select the entire tag with at.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.