I've been trying to explore code folding options in Vim, and I have stumbled upon this stack-overflow question. While I have been able to put that into my vimrc
, the problem I'm having is that it doesn't fold the multi-line comments above a function. How do I solve this?
If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines.
Opening and closing foldsThe command zc will close a fold (if the cursor is in an open fold), and zo will open a fold (if the cursor is in a closed fold). It's easier to just use za which will toggle the current fold (close it if it was open, or open it if it was closed).
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.
With this mapping, to create a fold, you can hit v to enter visual mode (or Shift-v to enter line-oriented visual mode), select some lines with the arrow keys, and hit spacebar to collapse the selected lines. To expand the lines, put the cursor over the fold and hit spacebar (in vim terminology, this deletes the fold).
The default syntax/c.vim
provides folding definitions. Unless you define a c_no_comment_fold
variable, this also folds multi-line comments.
To activate this, just put
:setlocal foldmethod=syntax
into ~/.vim/after/ftplugin/c.vim
.
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