70

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
  • 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). Commented Feb 13, 2015 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
    Commented Feb 14, 2015 at 0:02
  • However, the other question has exactly the same answer, so this is a duplicate.
    – Doorknob
    Commented Feb 14, 2015 at 0:04
  • Posted sample answer which won't fit into other question, so it's not duplicate then.
    – kenorb
    Commented Feb 14, 2015 at 0:20
  • 2
    @Doorknob The fact that two questions happen to have the same answer does not necessarily mean that the questions are duplicates of each other. Commented Feb 14, 2015 at 5:49

4 Answers 4

61

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.

1
47

You can jump between tags using visual operators, for 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:

4

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.

1

A relatively new and popular plugin vim-matchup provides quite extensive support as long as there is syntax highlighting for the language, HTML included.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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