Vim Folding ( Marker Method )

Hi, today i’ll be talking about folding for vim. Folding is to fold codes from line to line to have a better view on codes. today i will be demonstrating marker fold method for vim.

Firstly , you will need to set your foldmethod to marker code and the default for open and close marker is {{{ and }}}, you can change it as the command i show below. To have a permanent change you can place the command below into your .vimrc

:set foldmethod=marker
:set foldmarker=#StartFold,#EndFold

There are few command to take note of :-

za - To toggle fold
zd - To delete fold

In order to create a fold there are few ways

Range Folding

:1,10 fold

Block Folding

1.Position the cursor on the first brace, and type zfa} 
zfa} - to find the end } for the block .
2.You can also go either way by Positioning the cursor on the end brace and type zfa{
zfa{ - to find the start { for the block .
zf%  - to find start / end for the block.

Manual Folding

By typing the marker manually.
For this example the marker start is #StartFold and marker end is #EndFold

#StartFold
code goes here .......
#EndFold

SHORT CUTS!!!!!

zf#j creates a fold from the cursor down # lines.
zf/string creates a fold from the cursor to string.
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.

You may also like...