tags:

views:

554

answers:

3

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?

A: 

Search for "vim" and "folding" that will yield the content you search for

jitter
A: 

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

jheddings
+6  A: 

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.

jamessan
Need one more thing. Syntax works great but doesn't let me define zfa) so that I can collapse argument lists. I have some really long lists that I don't need to see what's in them and it would be really handy to collapse automatically but it won't let me do that in syntax mode and I have too many functions for define mode to be usable.
Andrew Redd
Unfortunately, you can't use multiple foldmethods at the same time and manual fold creation only works for marker/manual methods. You could copy syntax/c.vim from the system-wide directory to ~/.vim/syntax/c.vim and adding the "fold" keyword to the end of the cParen syntax definitions. The downside is that you'll need to do that whenever new major Vim releases are made so you don't lose any changes that are made upstream. It also requires that you put the opening "{" on a separate line, otherwise its fold will override the arg list fold.
jamessan