Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 622
Deesbek Avatar asked Aug 22 '15 09:08

Deesbek


3 Answers

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">------------------------------------
like image 111
adam_0 Avatar answered Nov 06 '22 09:11

adam_0


The feature you need is called "folding".

Basically, you tell Vim to use one of its available 'foldmethod' and manage each line's 'foldlevel' with commands like za, zr or zM. You can find all the details in :help folding.

Adding these lines to your vimrc should provide a good starting point:

augroup XML
    autocmd!
    autocmd FileType xml setlocal foldmethod=indent foldlevelstart=999 foldminlines=0
augroup END

With these settings, all folds should be open when you load a file, the fold level of each line should be derived from its indentation and you should be able to fold away single lines.

Use zm to fold more, zr to fold less, zc to close a fold, zo to open it, za to toggle between those two states, zM to close every fold, zR to open them and so on…

Documentation:

:help folding
:help 'foldmethod'
:help 'foldlevelstart'
:help 'foldminlines'
like image 45
romainl Avatar answered Nov 06 '22 09:11

romainl


This is taken from as @adam_0's answer, but using autocommand (as per @romani's answer), and with syntax on included as per @Paul's comment.

augroup XML
    autocmd!
    autocmd FileType xml let g:xml_syntax_folding=1
    autocmd FileType xml setlocal foldmethod=syntax
    autocmd FileType xml :syntax on
    autocmd FileType xml :%foldopen!
augroup END

The :%foldopen! line at the end opens all folds so that the file is WYSWIG when you load the file. But zM and friends are immediately available for folding if you want.

like image 10
ronen Avatar answered Nov 06 '22 11:11

ronen