HTML and XHTML built in support in Vim

For some reason, everytime I read something about Vim and (X)HTML support, guys are referring to many weird plugins and extra tools, which is often outdated.

Many folks do not know, that Vim 7+ has decent support for XML/XHTML/HTML languages (no plugins needed!) with possibilities to extend it with any XML-based language you want. What you can do is to use DTD/RNG converters that prepares Vim definition which is used to give you omni completion.

For example, my Vim installation contains support for HTML4 and XHTML languages by default:

    $ rpm -ql vim vim-common | grep xml

There is nothing to google, nothing to install. It just works. Vim documentation is better resource than Google most times.

The trick is Vim’s autoloading feature. You need to make sure the file you are opening has the proper DOCTYPE definition which is correct. So use that for HTML and XHTML files, then Vim 7+ will automatically enable XML/HTML omni completion for you. Example for HTML4:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

Now try to insert body tag, type “bo” and hit Ctrl X O. Bang. Try to add an attribute, type “on” and hit it again. Bingo.

You can use Ctrl X O and other features:

    - after "<" complete the tag name, depending on context
    - inside of a tag complete proper attributes
    - when an attribute has a limited number of possible values help to complete
    - complete names of entities (defined in |xml-omni-datafile| and in the
      current file with "<!ENTITY" declarations)
    - when used after "</" CTRL-X CTRL-O will close the last opened tag

More info (and possible user customization with own XML definitioins) here:

20 August 2012 | linux | fedora | vim