Skip to content

Folding setup

jcfaria edited this page Sep 15, 2026 · 3 revisions

Vim has several methods of folding text, and Vim-R adds one of its own.

Folding by syntax

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:

set nofoldenable

Notes:

  1. Enabling folding may slow down Vim.
  2. This is not a Vim-R option. It is a feature of syntax/r.vim, which is distributed with Vim itself.
  3. To turn it off, delete the line rather than set it to 0. Vim's syntax script tests the value before setting 'foldmethod', so let r_syntax_folding = 0 leaves syntax folding off — and Vim-R will then give you note folding if you asked for it. But the same script still creates its fold regions on the mere existence of the variable, which you pay for without getting a fold out of them. Delete the line to avoid that cost too.

Folding by the levels of the note comments

Vim-R can fold an R script by its note comments (#., #.., #...). It is off by default; to turn it on, put in your vimrc:

let R_note_folding = ['r']

The value is the list of file types in which the folds are created, and only r has any effect. This one is a Vim-R option, and it is mutually exclusive with syntax folding: if g:r_syntax_folding is non-zero, Vim-R does not fold by note levels and warns once. A value of 0 leaves syntax folding off, and note folding proceeds exactly as when the variable was never set. See :help R_note_folding, :help R_note_foldtext and :help R_note_foldlevel.

'foldmethod' is seized only if it still is manual, so a global 'foldmethod' of yours is left alone.

Folding other file types

If you edit other file types and want folding there too, that is a question about your editor and not about Vim-R. In Vim, pick one of the methods in :help folding. In Neovim, the built-in tree-sitter folding is:

vim.o.foldmethod = "expr"
vim.o.foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.o.foldenable = false

The older nvim_treesitter#foldexpr() requires the nvim-treesitter plugin; vim.treesitter.foldexpr() is part of Neovim.

Leave foldenable alone if you prefer all folds closed on startup.

Clone this wiki locally