Search code examples
vimfoldingfold

gvim automatic show foldcolumn when there are folds in a file


I know you can use

set foldcolumn=1

to enable fold column

but is there a way to automatic turn it on only when there are folds exist in the file?


Solution

  • Most probably you can create a function to check if the file has any folds, like:

    function HasFoldedLine() 
        let lnum=1 
        while lnum <= line("$") 
            if (foldclosed(lnum) > -1) 
                return 1 
            endif 
            let lnum+=1 
        endwhile 
        return 0 
     endfu 
    

    Now you can use it with some autocommand, e.g.:

    au CursorHold * if HasFoldedLine() == 1 | set fdc=1 | else |set fdc=0 | endif 
    

    HTH