closetag.vim : Functions and mappings to close open HTML/XML tags
| script karma
Downloaded by 27825
Comments, bugs, improvements
|This script eases redundant typing when writing html or xml files (even if you're very good with ctrl-p and ctrl-n :). Hitting ctrl-_ will initiate a search for the most recent open tag above that is not closed in the intervening space and then insert the matching close tag at the cursor. In normal mode, the close tag is inserted one character after cursor rather than at it, as if a<C-_> had been used. This allows putting close tags at the ends of lines while in normal mode, but disallows inserting them in the first column.
For HTML, a configurable list of tags are ignored in the matching process. By default, the following tags will not be matched and thus not closed automatically: Area, Base, Br, DD, DT, HR, Img, Input, LI, Link, Meta, P, and Param.
For XML, all tags must have a closing match or be terminated by />, as in <empty-element/>. These empty element tags are ignored for matching.
Comments are not currently handled very well, so commenting out HTML in certain ways may cause a "tag mismatch" message and no completion. ie, having '<!-- a href="blah">link!</a -->' between the cursor and the most recent open tag above doesn't work. Well matched tags in comments don't cause a problem.
|To use, place this file in your standard vim scripts directory, and source it while editing the file you wish to close tags in. If the filetype is not set or the file is some sort of template with embedded HTML, you may get HTML style tag matching by first setting the closetag_html_style global variable. Otherwise, the default is XML style tag matching.
For greater convenience, load this script in an autocommand:
:au Filetype html,xml,xsl source ~/.vim/scripts/closetag.vim
Also, set noignorecase for html files or edit b:unaryTagsStack to match your capitalization style. You may set this variable before or after loading the script, or simply change the file itself.
Rating scripts is only available on the
script versions (upload new version)
Click on the package to download.
ip used for rating: 22.214.171.124
||Bugfix release: Changed function names to be script-local to avoid conflicts with other scripts' stack implementations.
||Added synID()-based comment awareness:
Comment checking is now handled by vim's internal syntax checking. Closing tags outside comments only matches non-commented tags. Closing tags in comments matches only tags within comments, skipping blocks of normal code (wee!). Can be really slow if syn sync minlines is large. Set the b:closetag_disable_synID variable to disable comment contexts if it's too slow.
Normal mode closetag now uses <C-R> in insert mode rather than p in normal mode: Tag closing no longer clobbers the unnamed register, and (for vim 6.0) Doesn't modify the undo buffer when no change was made. Made closetag_html_style variable buffer-local. Improved documentation (hopefully).
Configuration Variables rundown:
- b:unaryTagsStack Buffer local string containing a whitespace seperated list of element names that should be ignored while finding matching closetags. Checking is done according to the current setting of the ignorecase option.
- b:closetag_html_style Define this (as with let b:closetag_html_style=1) and source the script again to set the unaryTagsStack to its default value for html.
- b:closetag_disable_synID Define this to disable comment checking if tag closing is too slow.