Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM syntax folding : disable folding multi-line comments

Tags:

c++

vim

I use the "syntax" foldmethod in vim 7.3. In .vimrc:

set foldmethod=syntax

When I open Test.cpp, containing:

/* A function with a multi-line
 * comment. This takes at least
 * four lines and I want to be
 * able to read all of them.
 */
void TheFunction()
{
  DoStuff();
}

I see the following when folded:

+--  5 lines: A function with a multi-line---------------------------------------------
void TheFunction() 
+--  3 lines: {------------------------------------------------------------------------

I like the function body folding, but not the comment-folding. I want to disable it so it looks like this:

/* A function with a multi-line
 * comment. This takes at least
 * four lines and I want to be
 * able to read all of them.
 */
void TheFunction() 
+--  3 lines: {------------------------------------------------------------------------

How do I do this? I can see the syntax group that's relevant with :syn list cComment

cComment       xxx matchgroup=cCommentStart start=+/\*+ end=+\*/+  extend fold contains
=@cCommentGroup,cCommentStartError,cSpaceError,@Spell
                   links to Comment

But tooling around for an hour with the vim documentation and google hasn't told me how to remove the "fold" attribute from this group.

Is my only recourse really to edit the language syntax file? I suppose it's less ugly to copy the system syntax file and use this, but I should be able to turn off a specific group with a command in my .vimrc.

like image 723
reasgt Avatar asked Apr 06 '12 02:04

reasgt


People also ask

How do you unfold lines in Vim?

However, if you'd like to open all of the nested folds on that line, use zO — Vim will open all of the folds. Running zc in normal mode will close all the folds after using zO . The zr command will open all first-level folds in Vim, and zR will open all of the folds in the file.

What is code folding in vim?

Yanis. March 15, 2020. Folding is a way to hide a chunk of text, and thus unclutter the view. That comes handy when you need to focus on some other part of code, or if you want to get a quick overview of the whole file.

How do I fold a method 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 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

When 'foldmethod' is set to "syntax" then /* */ comments and { } blocks will become a fold. If you don't want comments to become a fold use:

:let c_no_comment_fold = 1
like image 134
kev Avatar answered Oct 21 '22 09:10

kev