Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapse comments and all functions in vim/gvim

Tags:

vim

folding

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?

like image 999
Andrew Redd Avatar asked Nov 16 '09 19:11

Andrew Redd


People also ask

How do I collapse a row in Vim?

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.

How do I expand all lines in Vim?

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.

How do I save a fold in Vim?

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.


1 Answers

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.

like image 185
jamessan Avatar answered Sep 21 '22 12:09

jamessan