31

Is there a shortcut to jump from, e.g, <XX> to </XX>?

I already gave it a try with %, but it doesn't work in this situation.

Matthias Braun
  • 7,797
  • 7
  • 45
  • 54
daisy
  • 53,527
  • 78
  • 236
  • 383
  • 2
    I think you'll need a plugin like [matchit](http://www.vim.org/scripts/script.php?script_id=39) in order to be able to match XML tags. `matchit` itself seems to come with most `vim` distributions, but you'll still have to enable it yourself. `:help matchit-install` for more. – jw013 May 09 '12 at 00:51
  • Related: [How to jump between matching HTML/XML tags?](http://vi.stackexchange.com/q/780/467) at Vim SE – kenorb Feb 14 '15 at 00:29

2 Answers2

43

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 the 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 the opposite tag.

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


To record that action into a register, press qq to start recording, perform the tag jump as above (including Esc), press q to finish. Then to invoke the 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 a plugin such as matchit.vim (See: Using % in languages without curly braces).


See also:

Matthias Braun
  • 7,797
  • 7
  • 45
  • 54
kenorb
  • 20,250
  • 14
  • 140
  • 164
15
  • Install the matchit plugin (see :help matchit-install for instructions).
  • Make sure automatic file-type detection and plugin-loading is enabled (:filetype plugin on).

Henceforth, whenever you edit a file detected as xml or html or some other tag-based markup language, the combination of the matchit plugin and the filetype plugin files will allow the % motion to match open and close tags.

jw013
  • 50,274
  • 9
  • 137
  • 141