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.
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">------------------------------------
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With