0

I often use zC to close nested folds under the cursor. However this command is not useful in files with a single, global level-zero fold (e.g. fortran program...end program and latex \begin{document}...\end{document}). In these files I typically use setlocal foldlevel=1 and rarely want to fold below that level.

Are there any commands or functions I can use to recursively close folds under the cursor only up to the current fold level? Perhaps this needs a custom function?

1 Answer 1

1

Alright this wasn't too difficult. Adding this to vimrc does the trick:

" Nested close function
function! s:close_nested() abort
  let line = line('.')
  while line > 1 && foldlevel(line - 1) > &l:foldlevel
    let line -= 1  " stop when preceding line matches desired level
  endwhile
  exe line . 'foldclose'
endfunction

" Nested close mapping
" Note: When called on line below fold level this will still trigger a close. So
" pressing e.g. 'zCzC' will first fold up to foldlevel then fold additional levels.
noremap zC <Cmd>call s:close_nested()<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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