Alot of people I know don't know vims folding feature.
Remember this will not work with original vi, only if compiled with folding support and
version higher or equal than 6.0.With folding it is possible to summarize a block of code or other information to a folder
which fits in one line.
To use folding for example mark a few lines in vims visual mode (
Esc-shift-V).
Before using folding:
#include <stdio.h>
int main (void) {
printf("Hello World\n");
return 0;}
Now press z+f to summ up the marked lines in a folder.
After using folding:
#include <stdio.h>
-- 4 Zeilen: int main (void) {----------------------------
To open the folder press
z+o. If you want to open all folders in a file press
z+O.To delete a folder press
z+e to delete all folders press
z+E.The folders will not be written to the file, if you open the file with cat or less the
code will be displayed without any folder.
You can also write files using folders manually.
A folder is just a tag which tells vim to display something as a folder. So it is also possible to write a file with folders.
To mark the start of a folder just use
{{{ (for example in a comment of a source file)
and
}}} to mark the end of the folder.
If you open the file now in vim again it will not display the folder because you
have to specify the folder method in the file.
As folder methods you can use:manual manually define folds
indent more indent means a higher fold level
expr specify an expression to define folds
syntax folds defined by syntax highlighting
diff folds for unchanged text
marker folds defined by markers in the text
So in my example I will add vim:foldmethod=marker somewhere in the file and the file will have
all folders when opening it. Vim will add the first line of the folder for the folder description
(int main (void) in the first example).
Important keys (imho) when working with folding:zf create folder
zd delete folder under cursor
zD delete folder recursive
zE delete all folders in file
zo open folder under curso
zO open folder recursive
zc close folder under cursor
zC same as zc recursive
za open folder if closed, close folder if opened
Vims folding has got many more features which would bloat this blog entry here. I hope
I described the features I am using most, if you want to have an overview about what
is also possible with folding don't hesitate to type :help fold in vim.