Skip to content

Instantly share code, notes, and snippets.

@kshenoy
Last active December 15, 2015 18:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kshenoy/5302176 to your computer and use it in GitHub Desktop.
Save kshenoy/5302176 to your computer and use it in GitHub Desktop.
Function to open folds that have less than the specified number of lines
function! FoldAllBut( foldminlines )
" Description: Function to open folds that have less than the specified number of lines
" We assume that the folds are initially closed
" If a fold exists and is closed and has lesser number of lines than specified, open it and all nested folds
" Note: This does not work on nested folds
folddoclosed
\ if (( foldclosed(".") >= 0 ) && ( foldclosedend(".") - foldclosed(".") + 1 < a:foldminlines ))
\ exe 'normal! zO'
\ endif
endfunction
@kshenoy
Copy link
Author

kshenoy commented Apr 4, 2013

The silent! is required to ignore a trigger to search for the last searched pattern. TBH, I'm unable to figure out why this happens.
Thanks to dhruvsagar for suggesting using folddoclosed()

@dhruvasagar
Copy link

Hey kshenoy, chrisbra10 pointed out the reason why it was triggering the search here on reddit. I've updated my fork of your gist here with the fix accordingly.

function! FoldAllBut( foldminlines )
    folddoclosed 
        \ if ( (foldclosed(".") >= 0 ) && ( foldclosedend(".") - foldclosed(".") + 1 < a:foldminlines ))
        \   exe "normal! zO"
        \ endif
endfunction

Posted here just in case.

@kshenoy
Copy link
Author

kshenoy commented Apr 4, 2013

Ah, works properly now. Updated it at my end as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment