Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim folding - Close all children recursively under cursor

Tags:

vim

folding

I know how to open all folds under cursor with zO.

But how to do reverse?

I want something like za does, but also with recursivity.

PS. I know there is zC, but it closes all parent folds in relation to current line and I want to close children.

like image 784
rofrol Avatar asked Feb 07 '14 17:02

rofrol


People also ask

How do you toggle fold in Vim?

With the following in your vimrc, you can toggle folds open/closed by pressing F9. In addition, if you have :set foldmethod=manual , you can visually select some lines, then press F9 to create a fold. Here is an alternative procedure: In normal mode, press Space to toggle the current fold open/closed.

How does folding work 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.

What is code folding in Vim?

Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text while viewing only those subsections that are currently of interest.

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.


1 Answers

Note: the question deals with only current children. I.e to close/open till current fold level, and NOT all the way as zC, zO, zA, zM, zR.

Fold only current children recursive

zx folds: Undo manually opened and closed folds: re-apply 'foldlevel', then do "zv": View cursor line. Also forces recomputing folds. This is useful when using 'foldexpr' and the buffer is changed in a way that results in folds not to be updated properly.

zc close folded text

I think you want to combine both:

  1. go to a line in father folding level
  2. Press (in normal mode) zxzc. Or remap it: nnoremap zxc zxzc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Unfold only current children recursive

Btw: to do the opposite, open recursive all children: zczA

Credits: https://vi.stackexchange.com/a/16046/29452

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Un-/Fold only selected recursive

Or to open/close a desired range of folds recursive:

  1. Select the range visually.
  2. :foldo (as previous link) or :foldc (as @majkinetor answer).

Tips to select the range visually:

]z jump to end of current open fold.
[z jump to beginning of current open fold.
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.

To jump through closed folds in Vim: https://stackoverflow.com/a/9407015/9391770 (I did not test it yet)

like image 123
Xopi García Avatar answered Sep 28 '22 01:09

Xopi García