Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fold C++-style comments in Vim?

Tags:

vim

Syntax folding in Vim makes it easy to create folds for regions, the start and end of which can be defined with regular expressions:

syn region myRegion start="#region" end="#endregion" transparent keepend extend fold

However, I am not sure how I can use syntax folding to define folds for C++-style comments, which are sets of lines beginning with "//".

like image 482
Don Reba Avatar asked Feb 28 '23 10:02

Don Reba


1 Answers

I think I found a solution:

:syn match comment "\v(^\s*//.*\n)+" fold

This is a multiline match that folds consecutive lines beginning with whitespace and double slashes. The "\v" in the beginning labels the pattern "very magic" for brevity. Seems to work.

like image 111
Don Reba Avatar answered Mar 07 '23 16:03

Don Reba