5

I'm using plugin Chiel92/vim-autoformat auto formatting xml files. So the default format program used for xml file is tidy. The way it formats xml file is like below:

<List id='list' items='{/sss}' mode='{device&gt;/listMode}' noDataText='{i18n&gt;masterListNoDataText}' select='onSelect' growing='true' growingScrollToLoad='true'>
    <items>
        <ObjectListItem type='{device&gt;/listItemType}' press='onSelect' title='{Name}' number='{ path: "Price", formatter: "exp.ind.cli.util.Formatter.currencyValue" }' numberUnit='USD'></ObjectListItem>
    </items>
</List>

As you can see the attributes of the tags are on one line, and it can last very long even goes out of the screen. What I want is all the attributes to be formatted on separate lines( vertically aligned ).

<List id='list' 
    items='{/sss}' 
    mode='{device&gt;/listMode}' 
    noDataText='{i18n&gt;masterListNoDataText}' 
    select='onSelect' 
    growing='true' 
    growingScrollToLoad='true'>

    <items>
        <ObjectListItem type='{device&gt;/listItemType}' 
                    press='onSelect' 
                    title='{Name}' 
                    number='{ path: "Price", formatter: "exp.ind.cli.util.Formatter.currencyValue" }' 
                    numberUnit='USD'>
        </ObjectListItem>
    </items>
</List>

I think it makes easier to spot the attributes and improve the readability of the xml file. Is there any way to achieve this?

  • 6
    "vim is not formatting them correctly" No, Vim formats them correctly. What it doesn't do is format them how you want. For that you will need to find a specialized command-line xml formatter and use it from Vim either with :!<external command> or with :help 'formatprg' or :help equalprg. – romainl May 3 '15 at 7:25
  • It's automatic code format I'm talking about. Even if I manually type a line break, after auto xml format, everything goes back to the original format. – Aaron Shen May 3 '15 at 12:34
  • What are you using to automatically format the xml? – EvergreenTree May 3 '15 at 16:20
  • I'm not clear what is vim currently automatically formatting xml, I installed the Chiel92/vim-autoformat plugin, and I think for xml files, it will use "tidy" program bundled with mac os to format it. – Aaron Shen May 3 '15 at 23:03
  • I don't know why I'm getting down votes. Does anyone have a solution for this problem? or I'm just the only one who wants this? – Aaron Shen May 4 '15 at 0:24
7

This should almost work, it aligns the attributes slightly different than your example, but it's probably "close enough":

autocmd FileType xml let g:formatprg_args_expr_xml .= '." --indent-attributes 1"'

Which will format it like as:

<List id='list'
      items='{/sss}'
      mode='{device&gt;/listMode}'
      noDataText='{i18n&gt;masterListNoDataText}'
      select='onSelect'
      growing='true'
      growingScrollToLoad='true'>
    <items>
        <ObjectListItem type='{device&gt;/listItemType}'
                        press='onSelect'
                        title='{Name}'
                        number='{ path: "Price", formatter: "exp.ind.cli.util.Formatter.currencyValue" }'
                        numberUnit='USD'></ObjectListItem>
    </items>
</List>

Now, figuring this out was actually quite easy :-) Here's what I did:

  1. I looked at the vim-autoformat homepage to see how it formatted XML files, and it says: "tidy for XHTML and XML".

  2. So, we need to see if the tidy tool supports formatting the attributes like you want. After installing tidy, I ran tidy -h to see the help for this program. This told me that -help-config lists all configuration options, so I ran that:

    $ tidy -help-config
    

    This gave me a very long list, so I put the output in Vim:

    $ tidy -help-config | vim -
    
  3. Since we want to do something with attributes, I used /attr to search for that, and after pressing n once I get:

    indent-attributes           Boolean    y/n, yes/no, t/f, true/false, 1/0
    

    I have no idea what this does, but it looks like it might be what we want?

  4. Let's try adding this option. I went back to the vim-autoformat homepage, and noticed the "How can I change the behaviour of formatters, or add one myself?" section, which is how I figured out how to add the above option (this is the only part that required some effort, since the section isn't brilliantly written).

  • Thanks Carpetsmoker, thanks for even teaching me how to find solution for problems. It's very useful. – Aaron Shen May 5 '15 at 0:05
1

The suggested solution didn't work for me. formatprg_args_expr_xml is no longer recognized in the current version (at 713e7c2).

I solved the issue by adding these two lines in my .vimrc.

let g:formatdef_fmt_custom_xml = '"tidy -xml -q --show-errors 0 --show-warnings 0 --indent-attributes 1"'
let g:formatters_xml = ['fmt_custom_xml']

Of course I had to make sure tidy is already installed and present in my $PATH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.