11

I have a correctly formatted xml file, and following the command given as an answer here:

How can I autoformat/indent C code in vim?

I am trying to auto indent my file to display correct nesting.

Before I tried to use this command I set the file type to xml using :set ft=xml as the file I started with has an extension of .mm, and also :set nowrap.

Here is my ~/.vimrc file:

syntax on
set history=1000
set smartindent
set tabstop=2
set shiftwidth=2
set expandtab

How come when I issue gg=G, I get a message saying 54 lines indented, yet my file remains un-nested?

Community
  • 1
  • 1
grimetime
  • 390
  • 1
  • 4
  • 12
  • 1
    Not a direct answer, but have you tried [xmllint](http://xmlsoft.org/xmllint.html) --format? – ephemient Jul 25 '12 at 17:42
  • No, I haven't, I don't really have an inherent need to have the file formatted at this moment - I'm really just looking for the reason why this command isn't doing what I expect it to in vim. Thanks for the suggestion though. – grimetime Jul 25 '12 at 17:50

2 Answers2

21

in case you wanna try Vim's own XML indenter thing, you can...

:filetype indent on         (load indent files for specific file types etc.)
:e                          (to reload the current buffer)

this will load the vimscript at $VIMRUNTIME/indent/xml.vim

then when you do

:set indentexpr?        

...it'll say indentexpr=XmlIndentGet(v:lnum, 1)

~~

xmllint is better though, see... http://vim.wikia.com/wiki/VimTip349

i have handy keybinding like this for it in my .vimrc!

" one or more lines:
vmap ,px !xmllint --format -<CR>

" pretty-print current line
nmap ,px !!xmllint --format -<CR>
David Lam
  • 4,689
  • 3
  • 23
  • 34
  • 1
    I'm curious why you feel xmllint is better. In my experience, it is overly aggressive in pruning out blank newlines and comments, whereas Vim's built-in XML indenter _only_ indents the leading whitespace as expected. As an aside to those interested: I had loads of trouble getting Vim's built-in XML formatter to work at all, until I adopted tpope's [vim-sensible](https://github.com/tpope/vim-sensible) defaults, at which point everything "just worked" including vim's equals (=) operator. See also http://stackoverflow.com/a/28365920/1207769 – ctrueden Feb 06 '15 at 12:39
6

Try typing: :set equalprg?. If it says equalprg= it means that you do not have a program set for xml indentation, so it's probably doing some dumb default action. Here's a guide for setting xmllint as your formatter: http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html

Matt Boehm
  • 1,894
  • 1
  • 18
  • 21
  • Thanks! That worked great. I was watching one of [Derek Wyatt's vim Screencasts](http://vimeo.com/15443936) and just assumed that vim knows how to nest xml on it's own. – grimetime Jul 25 '12 at 18:29
  • 2
    Great reference to the guide on setting up xmllint as a formatter. Formatting XML can be a very personalized thing... for example, you may not want to indent every tag in all situations or you may want text wrapping for long text() nodes. So an alternative to xmllint could be [xmlformat](http://www.kitebird.com/software/xmlformat/)! – darcyparker Jul 25 '12 at 23:19