Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fold C code with vim?

Tags:

c

vim

folding

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?

like image 708
chutsu Avatar asked Nov 17 '12 16:11

chutsu


People also ask

How do I create a fold in Vim?

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.

How do I collapse a line of code in Vim?

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).

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.

How do I expand all lines in Vim?

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).


1 Answers

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.

like image 159
Ingo Karkat Avatar answered Sep 22 '22 17:09

Ingo Karkat