Question

I'm a novice user to vim and I haven't been able to find in the internet how to collapse functions and I've figured out how to collapse the argument list in C with zfa} and zfa). but I can't figure out how to collapse the comments sections. How do I do that?

Second question but related, is there a way to collapse all functions/argument lists/comments in a file at the same time?

Was it helpful?

Solution

The functionality you're referring to is called "folding" (see :help usr_28). The zf command is used to manually create a fold and is only used if the foldmethod option is set to either "marker" or "manual". In the examples you gave, you're creating folds over the regions specified by the a} and a) text objects (see :help usr_4.8).

For C, you can setlocal foldmethod=syntax and the folding regions will be automatically determined by the syntax rules. This should only be done for C files by either putting the setting in ~/.vim/ftplugin/c.vim or putting the following autocmd in your ~/.vimrc.

autocmd FileType c setlocal foldmethod=syntax

N.B. both of those require that filetype detection is enabled (filetype on), and the ftplugin solution requires that filetype plugins are enabled (filetype plugin on). The latter is a superset of the former, so you don't need both commands in your ~/.vimrc.

As for opening/closing all folds in the current buffer, those are the zR and zM commands respectively.

OTHER TIPS

Add the following settings to ~/.gvimrc:

"folding settings
set foldmethod=indent   "fold based on indent
set foldnestmax=10      "deepest fold is 10 levels
set nofoldenable        "dont fold by default
set foldlevel=1         "this is just what i use

Then you can toggle folding with za. You can fold everything with zM and unfold everything with zR. zm and zr can be used to get those folds just right. Always remember the almighty help file at “help :folding” if you get stuck.

Source: http://smartic.us/2009/04/06/code-folding-in-vim/

Assuming you have set up your fold regions how you want them, set foldlevel to the desired level.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top