dotancohen
dotancohen

Reputation: 31481

VIM: Collapse all the code blocks inside my current block

In a large PHP application, I would like to collapse all the code blocks inside my current block, but not the current block itself. For instance:

if ($something) {

    if ($another) {
        // some code;
    }

    | <--THIS IS MY CURRENT CURSOR POSITION

    if ($yetAnother) {
        // more code;
    }

    if ($stillAnother) {
        // yet more code;
    }

}

How can I collapse the three inner ifs but not the outer if?

Thanks!

Upvotes: 3

Views: 3876

Answers (3)

ib.
ib.

Reputation: 28944

If I got the question right, the change in folding described in the statement is equivalent to the following sequence of actions.

  1. Close the current fold (inside which the cursor is located).
  2. Recursively close all of the folds inside the just closed one.
  3. Open the current fold keeping the inner folds closed.

The mapping

:nnoremap <silent> <leader>f m`zcVzCzo``

consequently runs the commands corresponding to the aforementioned steps, saving the cursor position before accomplishing them, and restoring it after.

Upvotes: 2

Walter
Walter

Reputation: 7981

The closest thing I can come up with is zMzv. That will close all folds (zM) and then open just enough folds to view the line your cursor is on (zv). You can map that to a shorter command if necessary.

It's not exactly what you asked for, since it will close all folds outside of your outer if statement as well.

Upvotes: 3

holygeek
holygeek

Reputation: 16185

See if this works:

:set foldmethod=marker foldmarker={,} foldlevel=2

Upvotes: 8

Related Questions