Vim : Match all bracket { and fold them (zf%)

Hello !

I'm using a lot the command zf% with the cursor on the first bracket of a function for example, to fold it.

Ex : I put my cursor on the first bracket

myfunction(){
# body
}

I type zf%

It become :

+--  9 lines : myfunction() {------------------------------------------------------------

I would like to fold all matching bracket in the file with 1 command.
Now i have to manually place my cursor on every bracket to manualy create the fold.

How to procced please ?

Thank you in advance.

Hello,

i found a way to do that easy by using a macro.

Type 'q' to begin recording the macro, then select a register between a-z. Now type the macro '/{' + 'zf%'. Press q at the end to finish recording.
Now you can do something like '10@q' to play 10 times this macro. I still need to improve it to make it begin at the top of the file and end at the bottom, but it's already a quick easy way to deserve the purpose.

Summary :
Create macro

qa/{<Enter>zf%q

Execute macro

10@a

Just be careful to not doing zf multiple times on the same {

Enjoy

EDIT : I'm here again, just call :set nowrapscan to configure search not going back to the beggining of the file. Then you can simply do 1000@a end all functions gonna be collapse 1 time.

1 Like

Thanks for sharing!