sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #583: Vim as XML Editor

 tip karma   Rating 695/201, Viewed by 14049 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 10, 2003 4:14      complexity:   intermediate
author:   Tobi      as of Vim:   6.0


If Vim is your main text editor, and if you do a lot of XML editing, then this mini howto might help you to make that even more fun:

Vim as XML Editor
http://www.pinkjuice.com/howto/vimxml/


 rate this tip  Life Changing Helpful Unfulfilling 

<< Quick way to write to your file. | Awesome text formatter >>

Additional Notes

tobiasreif pinkjuice com, October 13, 2003 3:41
I appreciate any feedback. Error reports are especially welcome.
email: tobiasreif pinkjuice com

Tobi
william at knowmad dot com, December 29, 2003 14:15
Tobias,

This is a most excellent tutorial! Many thanks for your work in putting this documentation together. I hope that others find it as useful.

Do you have any info about using Vim to edit DocBook files? It'd be nice to have folding support. Is this possible?

Thanks,
William
tobiasreif pinkjuice com, December 31, 2003 8:54
Hi William,

thank you very much for the kind words.

> Do you have any info about using Vim to edit DocBook files?

Various sections use DocBook as example language , see
  http://www.pinkjuice.com/howto/vimxml/tasks.xml
and
  http://www.pinkjuice.com/howto/vimxml/moretasks.xml

I recommend to set up OASIS catalogs, see
  http://www.pinkjuice.com/howto/vimxml/setup.xml#catalogs

If you have more specific questions feel free to post them here or to send them via email.

> It'd be nice to have folding support. Is this possible?

Yes, folding is discussed shortly in
  http://www.pinkjuice.com/howto/vimxml/tasks.xml#creatingdocuments

See
  http://www.pinkjuice.com/howto/vimxml/setup.xml#vimrc
under
  map <Leader>x
for stuff to set:
  :set filetype=xml<CR>
  \:source $VIMRUNTIME/syntax/xml.vim<CR>
  \:set foldmethod=syntax<CR>
  [...]

Tobi
[email protected], November 19, 2004 0:15
Can Vim suggest valid tags based on context?

Thx for the tutorial
tobi, March 12, 2005 3:10
http://www.pinkjuice.com/howto/vimxml/intro.xml

"Vim is a text editor, and even when extended with some XML specific functionality, it is not a full-blown XML editor: For example, there's no tags- or WYSIWYG view, and no real structure/tree view. I don't really miss entry help (context-sensitive suggestions for completion), but this and other schema-driven (DTD, WXS, RNG, etc) functionality found in editors like nXML could be implemented using Vim's Python interface plus libxml's Python bindings, for example."
jason dot barnett [at] telesuite dot com, March 15, 2005 9:26
Overall, the tutorial is good.  I'd probably give it a 9 out of 10.  For users that are already familiar with Linux style commands the tutorial is extremely good.

I know that you say you abhor associating file extensions with content-types (which makes sense given your background).  However, in a Windows environment I find it very convenient to use file extensions and most Windows users are probably used to this idea anyways... .doc files are Word, .xls is Excel, etc.

Since you know the command line argument it isn't too hard to create the file associations.  To set up (for example) an association for xmlstar on Windows 2000 you do the following:
- Open up "My Computer"
- Go to Tools -> Folder Options -> (tab) File Types
- Find XML in the list (you can substitute any extension here)
- Under "Details" in the box below see if "Advanced" is an option.  If it is not, then write down what the current program name is and hit "Restore"
- Under "Details" choose "Advanced"
- Select the "New" button
- In the "Action" box this is what shows up in the context menu.  A good name here is XMLSTAR.
- In the "Application used to perform this action" box you type the following:
cmd.exe /k "C:\path\to\xmlstar\xml.exe -flags_go_here %1" but you replace the flags_go_here with whatever flags you want.  

*Note* the variable %1 in the file types menu is going to be the name of the XML file and AFAIK you can't pass any other command line options this way.  But, this gives you a quick and easy way to open up a file with XMLSTAR.  You can of course modify what I've said above to enable VIM for .txt files, or xmllint for XML files, or ...
[email protected], March 29, 2005 22:50
Hi,

If u want to just check the wellformedness of the xml document then from the vi editor u can use

!xmllint --noout %

for more on this command just give

:!xmllint
on opening an xml file using vi or vim editors
[email protected], April 22, 2005 13:35
I'm having trouble using folding with the XML syntax file distributed in 6.3.n.

It says to use:
    let g:xml_syntax_folding = 1
but that doesn't seem to work for me (no fold found).

The workaround  - which is still pretty good - is to keep your XML in shape with xmllint. Not sure it would work well with CDATA, though.

[email protected], May 16, 2006 16:23
A few days ago I had syntax folding working and I got really used to editing XML files with folding enabled.

Then I must have changed something in my .vimrc, and now I can't get syntax folding to work :(. (indent folding still works, but not what i need). This is in 6.3.46 and 6.4 (different machines, same results)

The following properties are set - I just can't wrap my head around why this wouldn't work:

filetype=xml
syntax=xml
foldmethod=syntax


here's my vimrc:
set tabstop=4
set shiftwidth=4
set vb
set autoindent
set cindent
syn on
set incsearch
set hlsearch
set confirm
set foldmethod=syntax
set history=100
set path+=/home/me/**
set suffixesadd+=.java
set isfname-=.
colorscheme torte

Any help would be greatly appreciated.
[email protected], May 17, 2006 14:01
I can't get the vim.xml in $ADDED loaded, I have to put it in my ~/.vim/plugin which force me to have it loaded automatically, each time I open .xml file. There is no error, it is just that nothing from within this file is taken into account in my buffer (e.g., after typing ',x', I get "XML mode is on" but no completion of tags). I like the approach to be independent from the extension : could you help me get it working ?
Anonymous, August 3, 2006 7:21
Well done.
I'm working on openoffice xml files which are ugly oneliner.
Your tutorial saved hours of my time (:%!xmllint --format -)
THX!!!
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo