I'm using vim for my matlab editor. And It works nicely, except I cannot get folding to work.

I found my syntax file on github: matlab.vim

And I wanted to add folding.

I changed the line

syn region matlabMultilineComment   start=+%{+ end=+%}+ contains=matlabTodo,matlabTab

to

syn region matlabMultilineComment   start=+%{+ end=+%}+ fold contains=matlabTodo,matlabTab 

And this works, zm now folds multiline comments!. So now I wanted to add folding for blocks

Starting on:

  • if
  • for
  • switch
  • while

ending on:

  • end and also between %% and %%

So I went from having

syn keyword matlabLabel         case switch
syn keyword matlabConditional       else elseif end if otherwise
syn keyword matlabRepeat        do for while

to have

syn keyword matlabLabel         case switch
syn keyword matlabConditional       else elseif if otherwise
syn keyword matlabEnd           end
syn keyword matlabRepeat        do for while
syn region matlabSection 
            \start=+%%+ 
            \end=+%%+ 
            \fold 
syn region matlabIfBlock        
            \matchgroup=matlabConditional 
            \start="if" 
            \end="end" 
            \transparent 
            \fold 

This however, does not fold neither my if statement, nor my %% section (my %{ %} multiline comment is folding nicely)

This answer is not complete, but might help:

The coolword in start="coolword" apparently cannot be a keyword to ommit this use the matchgroup ability, to mark the start word as a certain group. The endword can however be a keyword.

Also, ensure to use the \< and \> flags as described here: Syntax file: keywords within context Such that the words will not be taken out of context.

Now I have:

syn keyword matlabLabel         case
syn keyword matlabConditional       else elseif  otherwise
syn keyword matlabRepeat        do
syn keyword matlabFunction      error eval 
syn region matlabSection
            \ start="%%"
            \ end="%%"
            \ fold
syn region matlabIfBlock
            \ matchgroup=matlabConditional
            \ start="\<if\>"
            \ end="\<end\>"
            \ transparent
            \ fold
syn region matlabForLoop
            \ matchgroup=matlabRepeat
            \ start="\<for\>"
            \ end="\<end\>"
            \ transparent
            \ fold
syn region matlabForLoop
            \ matchgroup=matlabRepeat
            \ start="\<while\>"
            \ end="\<end\>"
            \ transparent
            \ fold
syn region matlabForLoop
            \ matchgroup=matlabRepeat
            \ start="\<parfor\>"
            \ end="\<end\>"
            \ transparent
            \ fold

syn region matlabSwitch
            \ matchgroup=matlabLabel
            \ start="\<switch\>"
            \ end="\<end\>"
            \ transparent
            \ fold
syn region matlabFunctionBlock
            \ matchgroup=matlabFunction
            \ start="\<function\>"
            \ end="\<end\>"
            \ transparent
            \ fold
"syn match matlabCellComment     "^.*$" 

the last line is commented out. It used to include %% and thus make the folding of %% sections impossible (And I'm too much of a vim noob to know what letting the remains be would do, so I eradicated it).

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.