Assume I have a syntax as exemplified below and that I want to create a syntax folding definition for the nested begin..end pairs.

begin : label
  a = 0;
    b = 1;
  c = 2;
    d = 3;

if (a) begin
  w = 0;
end else begin
  if (b) begin
    y = 0;
  end else if (c) begin : another_label
    y = 1;
  end else begin
    y = 2;

At the moment I have the following syntax defined, which has the obvious issue with the highlight of the end keyword and it also folds one line less when end is at column 1.

syn region blockContainer
    \ start="\<begin\>"
    \ end="\<end\>"
    \ skip="/[*/].*"
    \ transparent
    \ keepend extend
    \ containedin=ALLBUT,Comment
    \ contains=block
syn region block
    \ matchgroup=Statement
    \ start="\<begin\>"
    \ end="\<end\>"me=s-1
    \ transparent
    \ fold
    \ contained containedin=blockContainer
    \ contains=TOP

Ideally I would also like a second set of regions where folding only occurs for begin..end pairs where the begin has a label (: something).

I got to a point where I simply wasn't able to advance further with this syntax, so I started playing around with fold-expr. Unfortunately, the syntax is rather complex and the slowdown is quite noticeable when opening the files.

So, is any Vim syntax expert available to help me? :)

Your Answer


Browse other questions tagged or ask your own question.