23

When editing an XML file in IntelliJ IDEA, if the document references a schema, IDEA will fetch the schema and use the information for auto-complete. It knows which tags are valid in which contexts, so when you hit CTRL-space, it suggests only those tags. It also highlights any tags that are invalid according to the schema.

Does anybody know of an extension for Vim that provides similar functionality?

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
  • 1
    interesting, I'm tempted to have a go at it. Does it work in IDEA only for editing, or also for writing from scratch? That is, does it require that the entire document be valid; or that the part up to where you are editing be valid; or will it work with a bare fragment? This affects implementation. – 13ren Feb 25 '09 at 14:52

3 Answers3

15

VIM 7+ supports that by default without any plugins, it can load XML definition, there are converters for DTD and SchemaNG:

http://vimdoc.sourceforge.net/htmldoc/insert.html#ft-xml-omni

For example my Vim 7.3 already have definitions for XHTML:

$ rpm -ql vim vim-common | grep xml
/usr/share/vim/vim73/autoload/xml
/usr/share/vim/vim73/autoload/xml/html32.vim
/usr/share/vim/vim73/autoload/xml/html401f.vim
/usr/share/vim/vim73/autoload/xml/html401s.vim
/usr/share/vim/vim73/autoload/xml/html401t.vim
/usr/share/vim/vim73/autoload/xml/html40f.vim
/usr/share/vim/vim73/autoload/xml/html40s.vim
/usr/share/vim/vim73/autoload/xml/html40t.vim
/usr/share/vim/vim73/autoload/xml/xhtml10f.vim
/usr/share/vim/vim73/autoload/xml/xhtml10s.vim
/usr/share/vim/vim73/autoload/xml/xhtml10t.vim
/usr/share/vim/vim73/autoload/xml/xhtml11.vim
/usr/share/vim/vim73/autoload/xml/xsd.vim
/usr/share/vim/vim73/autoload/xml/xsl.vim
/usr/share/vim/vim73/autoload/xmlcomplete.vim
/usr/share/vim/vim73/compiler/xmllint.vim
/usr/share/vim/vim73/compiler/xmlwf.vim
/usr/share/vim/vim73/ftplugin/xml.vim
/usr/share/vim/vim73/indent/xml.vim
/usr/share/vim/vim73/syntax/docbkxml.vim
/usr/share/vim/vim73/syntax/xml.vim

Try it, it's worth it. Everything is documented clearly under the link above, I will not copy-paste it here.

lzap
  • 16,417
  • 12
  • 71
  • 108
  • Hm, what else can vim complete? – Raffi Khatchadourian Jun 24 '15 at 19:40
  • The out-of-box xml completion for Vim 7+ is nice, but I've never been able to get the vim2dtd script to actually work to make new definitions for other types of XML. The script hasn't been updated in nearly a decade. I'd kill to have the autocomplete files for dita xml. – Scribblemacher Sep 30 '15 at 14:46
  • Note this enables completion, but not schema validation. I.e. you're not going to get an error if you create a file that does not conform to your XML schema. – Dodgyrabbit Sep 29 '22 at 01:50
6

The closest thing that I've seen for Vim is the XML Completion script.

It contains XML definition files for DocBook 4.[234], XSL FO 1.0, XSLT 1.0, Relax NG 1.0, XML Schemas 1.0, SVG 1.1, XHTML 1.1, XInclude 1.0 and OpenOffice 1.0 XML format.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

Reference: http://ricardianambivalence.com/2013/02/17/xml-validation-in-vim/

With the bar.xml open, you can easily validate against foo.xsd. First, save your changes and then hit : and enter the following (it's the pipe to the terminal command).

:! xmllint --noout --schema foo.xsd bar.xml
icedwater
  • 4,701
  • 3
  • 35
  • 50
Soundararajan
  • 2,000
  • 21
  • 23