7

I'm working with vim on my linux machine and need code folding to work for R. I have the Vim-R-plusing2 installed to enable passing code from vim into an R session, but the folding does not work when foldmethod=syntax. How do I get folding for R working?

Andrew Redd
  • 4,632
  • 8
  • 40
  • 64

2 Answers2

7

What to do:

You need to add the following code to your vimrc file:

  let r_syntax_folding = 1 

Why it works:

This option changes how the Vim-R plugin loads.

This code is copied from syntax/vim.r and shows how the setting above changes what code is run:

" Error
if exists("g:r_syntax_folding")
  syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError fold
  syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError fold
  syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError fold
else
  syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError
  syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError
  syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError
endif  

Quoting from the help

10.8. Fold setup~

Vim has several methods of folding text (see |fold-methods| and |fold-commands|). To enable the syntax method of folding for R files, put in your |vimrc|:

let r_syntax_folding = 1

With the above option, Vim will load R files with all folds closed. If you prefer to start editing files with all folds open, put in your |vimrc|:

Community
  • 1
  • 1
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
  • FINALLY! Thank you. That was not actually the problem but you post told me what was. Comparing what was in your answer to what was in my code I found that the `syn region` lines omitted the fold option at the end. So folding wasn't turned on for those regions. It's fixed now. – Andrew Redd Jan 26 '11 at 16:53
1

Something similar, but referring to markdown, anyway you may find some useful info there.

You may try even :help folding for detailed descriptions.

Community
  • 1
  • 1
kfl62
  • 2,434
  • 4
  • 29
  • 40