Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's your favorite folding method (or secret techique) in Vim for HTML, Javascript and CSS? [closed]

Tags:

vim

fold

I use something like this: 1,40 fo but I think is not the most efficient way.

What's yours?

like image 613
alexchenco Avatar asked Jan 31 '10 09:01

alexchenco


People also ask

How do I fold a method in Vim?

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.

How do I collapse a line of code in Vim?

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).

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 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.

like image 50
Matteo Riva Avatar answered Sep 17 '22 16:09

Matteo Riva


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.

like image 43
michael Avatar answered Sep 19 '22 16:09

michael