Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fold/unfold HTML tags with Vim

Is there some plugin to fold HTML tags in Vim?
Or there is another way to setup a shortcut to fold or unfold html tags?
I would like to fold/unfold html tags just like I do with indentation folding.

like image 765
nsbm Avatar asked Aug 22 '11 13:08

nsbm


People also ask

How do I create a fold in Vim?

Open it in Vim, and place the cursor at the beginning of a paragraph. Make sure you're in normal mode, and type zf2j . After you press j , Vim will create a fold covering three lines — the line you started the fold on, and the next two lines.

How do I save a fold in Vim?

The problem is that when you close Vim, your artfully folded code returns to its unfolded state. The solution is quite simple - when you are ready to save your folds run the :mkview command. This will save your folds in the current buffer to your viewdir ( :h viewdir ) depending on your environment.


2 Answers

I have found zfat (or, equally, zfit) works well for folding with HTML documents. za will toggle (open or close) an existing fold. zR opens all the folds in the current document, zM effectively re-enables all existing folds marked in the document.

If you find yourself using folds extensively, you could make some handy keybindings for yourself in your .vimrc.

like image 163
James Lai Avatar answered Sep 23 '22 11:09

James Lai


If you indent your HTML the following should work:

set foldmethod=indent 

The problem with this, I find, is there are too many folds. To get around this I use zO and zc to open and close nested folds, respectively.

See help fold-indent for more information:

The folds are automatically defined by the indent of the lines.  The foldlevel is computed from the indent of the line, divided by the 'shiftwidth' (rounded down).  A sequence of lines with the same or higher fold level form a fold, with the lines with a higher level forming a nested fold.  The nesting of folds is limited with 'foldnestmax'.  Some lines are ignored and get the fold level of the line above or below it, whichever is lower.  These are empty or white lines and lines starting with a character in 'foldignore'.  White space is skipped before checking for characters in 'foldignore'.  For C use "#" to ignore preprocessor lines.  When you want to ignore lines in another way, use the 'expr' method.  The indent() function can be used in 'foldexpr' to get the indent of a line. 
like image 37
ihohbeto Avatar answered Sep 20 '22 11:09

ihohbeto