>
> Op woensdag 30 mei 2007, schreef wangxu:
> > I want to have this function:
> > formatting my xml file automatically.
> > or,indent xml element and attributes.
> >
> > is there any plugin like this?
> > thanks!
>
> Well, if you're on a unix machine with xmllint installed, you
> could do the following:
>
> setlocal equalprg=xmllint\ --format\ -
>
> and then use gg=G to reformat the entire document.
I don't have much luck with that technique.
If xmllint believes the xml isn't quite valid then it screws it up with
error messages and such.
I wrote this little function to do it for me. Works well enough.
Basically, I either run the command
:FormatSGML
Or, visually select some rows and run the command.
function! Format_SGML() range
" Add a new line to the bottom of the mark to be removed later
call cursor(a:lastline,1)
put =''
silent! exec "ma z"
" Add a new line above the mark to be removed later
call cursor(a:firstline,1)
put! = ''
silent! exec "ma y"
" Put each tag on a newline
exec line("'y").','.line("'z").'s/>\s*</>\r</ge'
" Reformat using Vim's indenter
call cursor(line("'y"),1)
exec 'normal! '.(line("'z")-line("'y")+1).'=='
" Delete the additional lines added
silent! exe "norm! 'ydd'zdd"
endfunction
command! -range=% -nargs=0 FormatSGML <line1>,<line2>call Format_SGML()
At least you have some additional options now.
HTH,
Dave