I use something like this: 1,40 fo
but I think is not the most efficient way.
What's yours?
Vim uses the same movement commands to define folds. Folding also works in visual mode. If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines. Another option is to specify a range in command mode.
The command zc will close a fold (if the cursor is in an open fold), and zo will open a fold (if the cursor is in a closed fold). It's easier to just use za which will toggle the current fold (close it if it was open, or open it if it was closed).
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.
I use foldmethod=marker
and have mappings to enter <!-- {{{ -->
and <!-- }}} -->
where I want the fold to start and end. I put the start marker on the line with the opening block tag like:
<div id="topmenu"> <!-- {{{ -->
so when it's folded I immediately see what the fold contains without the need to add extra comment.
For CSS it's even easier, I just use foldmarker={,}
and all definitions are automagically folded showing me just a very clear list of all classes, tags and ids which I can open just when I need them. Actually all my CSS files have this line at the very end:
/* vim: set fdm=marker fmr={,}: */
You can also visually select the region you want to fold and press zf
if you prefer.
I flip between indent and marker with this in my vimrc..
let g:FoldMethod = 0
map <leader>ff :call ToggleFold()<cr>
fun! ToggleFold()
if g:FoldMethod == 0
exe 'set foldmethod=indent'
let g:FoldMethod = 1
else
exe 'set foldmethod=marker'
let g:FoldMethod = 0
endif
endfun
Indent works ok for most beautified html but I use marker for large declarative table of contents style folding of documents. Depending on who wrote the file, one will work better than the other so you need quick access to both.
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