Sometimes the code is too long to look at, especially when reading the source code, you need to fold the code that you don't care about first to look at the code that you are most concerned about. Vim comes with the code folding function.
Foldmethod can be configured to define the folding mode. There are 6 optional methods: 1. Manual // manually define the folding mode.
2. indent // fold with indentation
3. expr // use an expression to define the fold
4. Syntax // use syntax highlighting to define collapse
5. Diff // fold Unchanged text
6. marker // I use syntax to define the folding with the flag. This method is relatively simple, but after this value is configured, open the code, vim collapsed all the code by default. This is obviously not what I want. After Google retries, find a solution. After setting foldlevelstart to 99, It is not collapsed by default. Configuration:
"Use syntax to highlight and define code folding
Set foldmethod = syntax
"Opening a file does not collapse the code by default.
Set foldlevelstart = 99
After the configuration is complete, you can enter the fold command on the method, class, if-Else, and various iterators on the desired block. Here, the simplest fold command is provided: ZC close fold Zo Open fold za open/close fold switch each other complete configuration of my code fold:
Set foldenable
Set foldmethod = Syntax "set syntax collapse
Set foldcolumn = 0 "set the width of the collapsed area
Setlocal foldlevel = 1 "set the number of folding layers
Set foldlevelstart = 99 "the Code is not folded by default when the file is opened.
"Set foldclose = all" is set to auto-disable fold
Nnoremap <space >@= (foldclosed (line ('.') <0 )? 'Zc': 'Z') <CR>
"Use the Space key to switch and collapse