Folding (expanding and colapsing) xml tags in vim (xml parsing)

Deesbek picture Deesbek · Aug 22, 2015 · Viewed 9.8k times · Source

I know that % in vim will jump from one tag to another, I also know how to use visual mode to select entire blocks between tags in an xml file, using o to jump from the top to the bottom tags.

I've done multiple web searches and have the matchit plugin installed using the following lines in my .vimrc:

filetype plugin on
runtime macros/matchit.vim

I've found this post, and have tried closetag.vim.

I review huge xml files and even with multiple monitors (and glasses) would find it useful to be able to collapse certain sections of the file.


Is there a way to collapse and expand sections between tags in MacVim or vim by default or through the use of plugins?

Answer

adam_0 picture adam_0 · May 18, 2017

The indent method almost worked for me, but I found the way it worked to be a little bit strange; essentially, it folded on the content of the tags instead of the tags themselves. This worked for me, from the Vim wiki:

let g:xml_syntax_folding=1
au FileType xml setlocal foldmethod=syntax

This method folds on the actual tags themselves, for example:

<MyLines group="first">
    <Foo value="1"/>
    <Foo value="2"/>
    <Foo value="3"/>
</MyLines>
<MyLines group="second">
    <Foo value="4"/>
    <Foo value="5"/>
    <Foo value="6"/>
</MyLines>

Looks like this after typing zc on line 1 or 5:

+--  5 lines: <MyLines group="first">------------------------------------
<MyLines group="second">
    <Foo value="4"/>
    <Foo value="5"/>
    <Foo value="6"/>
</MyLines>

Instead of:

+-- 10 lines: <MyLines group="first">------------------------------------