问题
I downloaded the Matchit plugin and used it on my HTML file. Everything worked as it was supposed to until I used it on my list.
<ol>
<li id="link-1"><a href="http://www.reddit.com/">reddit</a></li>
<li id="link-2"><a href="http://www.vim.org/">vim</a></li>
<li id="link-3"><a href="http://www.w3schools.com/">w3schools</a></li>
</ol>
If the cursor is on the first o when I press tab the cursor jumps to the first list item instead of the closing ol. And when I'm on the l of the first list item the cursor jumps to the l of the next list item tag. But if I move the cursor over to the first a of the link tag and hit tab then Matchit works just fine and jumps to the closing tag. Matchit works for all tags but list items? How do I fix this behavior?
回答1:
For starter, matchit is distributed with Vim without being activated. You don't need to download it, just follow the instructions at :help matchit-install.
Matchit uses a buffer variable called b:match_words to define pairs of matching tags: open an HTML file and type echo b:match_words. The value, here, is:
<:>,<\@<=[ou]l\>[^>]*\%(>\|$\):<\@<=li\>:<\@<=/[ou]l>,<\@<=dl\>[^>]*\%(>\|$\):<\@<=d[td]\>:<\@<=/dl>,<\@<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\@<=/\1>
If I understand :help matchit and these patterns correctly, the <\@<=[ou]l\>[^>]*\%(>\|$\):<\@<=li\>:<\@<=/[ou]l> part is where the "problem" is. Notice that the behaviour is the same for definition lists: instead of jumping to the end tag, matchit jumps to intermediary tags.
I'd guess that pasting this in an HTML ftplugin and doing a bit of customizing would solve the problem.
edit
This line seems to completely resolve the issue:
autocmd FileType html let b:match_words='<:>,<\@<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\@<=/\1>'
I've just removed the two totally unecessary pattern definitions for <ul|ol> and <dl>, leaving only the basic matches.
回答2:
Try using g%. Matchit has bad behavior: instead of jumping to the closing tag it iterates on the list of related matches. I do not know whether it was coded for HTML, but while editing vimscript it will iterate for->continue/break->endfor. g% makes matchit iterate in the opposite direction, thus if you are on the first item it will bring you to the last (for->endfor in my example).
By the way, matchit was included in vim distribution some years ago. You don’t need to download it, I can’t even say whether it is updated anywhere but in vim distribution.
来源:https://stackoverflow.com/questions/11851320/matchit-skips-to-next-list-item-in-html-instead-of-closing-tag