Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I find the close html tag quickly in vim?

Tags:

Just like editing C source file, I can press % to get the closing } for the current cursor's {. How can I do this when editing html files? Is there any shortcuts? To be clear, I want:

<html>
</html>

When the curosr moves to <html> , I want to press a key, so that the cursor will jump to </html>.

like image 311
Bin Chen Avatar asked Nov 07 '10 03:11

Bin Chen


People also ask

How do you identify a closing tag?

An opening tag begins a section of page content, and a closing tag ends it. For example, to markup a section of text as a paragraph, you would open the paragraph with an opening paragraph tag <p> and close it with a closing paragraph tag </p> (closing tags always proceed the element with a /).

Which html5 tag is self closing?

The br tag inserts a line break (not a paragraph break). This tag has no content, so it is self closing.


2 Answers

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 (See: Using % in languages without curly braces).


See also:

  • How to jump between matching HTML/XML tags? at Vim SE
  • Jump to matching XML tags in Vim at stackoverflow SE
  • Navigating HTML tags in Vim at stackoverflow SE
  • How to navigate between begin and end html tag? at superuser SE
  • VIM jump from one xml tag to the closing one at Unix SE
  • How can I select an html tag's content in Vim? at superuser SE
like image 112
kenorb Avatar answered Oct 01 '22 14:10

kenorb


You should be able to do this with the matchit plugin by typing % when your mouse is on the opening tag.

http://www.vim.org/scripts/script.php?script_id=39

like image 36
jmans Avatar answered Oct 01 '22 15:10

jmans