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?
With the following in your vimrc, you can toggle folds open/closed by pressing F9. In addition, if you have :set foldmethod=manual , you can visually select some lines, then press F9 to create a fold. Here is an alternative procedure: In normal mode, press Space to toggle the current fold open/closed.
To expand the lines, put the cursor over the fold and hit spacebar (in vim terminology, this deletes the fold). (Side note: you may want to change the look of collapsed folds.
The problem is that when you close Vim, your artfully folded code returns to its unfolded state. The solution is quite simple - when you are ready to save your folds run the :mkview command. This will save your folds in the current buffer to your viewdir ( :h viewdir ) depending on your environment.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With