1

I have trouble with one specific folding expression, s1. The documentation (:h fold-expr) says:

"s1", "s2", .. subtract one, two, .. from the fold level of the previous line

However this is not true for me. Create a file .vim/after/ftplugin/vim/test.vim/ with the following content:

"" This is a demo, the fold starts here. This line has foldlevel 1
" This line is folded wrong, it should have foldlevel 0, but has foldlevel 1
function! VimFolds(lnum)
  let thisline = getline(a:lnum)
  if match(thisline, '^""') >= 0
    return '>1'
  elseif match(thisline, '^" ') >= 0
    return 's1'
  else
    return '='
  endif
endfunction
setlocal foldmethod=expr
setlocal foldexpr=VimFolds(v:lnum)
set foldcolumn=3
set foldminlines=0

Open the file with Vim, it should be folded according to its own rules. The second line should be on foldlevel 0 according to documentation, but is on foldlevel 1.

Am I understanding something wrong here on how to use this?

  • 1
    looks like a bug to me – Christian Brabandt Nov 20 '15 at 9:25
  • Hm, indeed, 's1' seems to substract one at the next line. I asked vim_dev for clarification. I would say it is a bug. – Christian Brabandt Nov 20 '15 at 9:41
  • Ah ok, you already reported it, I will not file an issue on Github then. – cbaumhardt Nov 20 '15 at 9:44
  • it's a feature. documentation wil be updated – Christian Brabandt Nov 25 '15 at 7:51
1
"" This is a demo, the fold starts here. This line has foldlevel 1
" This line is folded wrong, it should have foldlevel 0, but has foldlevel 1
function! VimFolds(lnum)
  let thisline = getline(a:lnum)
  if thisline =~ '^""'
    return '>1'
  elseif thisline =~ '^" '
    return foldlevel(a:lnum-1)-1
  else
    return foldlevel(a:lnum-1)
  endif
endfunction
set foldmethod=expr
set foldexpr=VimFolds(v:lnum)
set foldcolumn=3
set foldminlines=0

I don't know how to make the 's1' value work as intended either.
However the code above seems to do what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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