Skip to content

Instantly share code, notes, and snippets.

@sjl
Created August 15, 2012 15:20
Show Gist options
  • Save sjl/3360978 to your computer and use it in GitHub Desktop.
Save sjl/3360978 to your computer and use it in GitHub Desktop.
function! MyFoldText() " {{{
let line = getline(v:foldstart)
let nucolwidth = &fdc + &number * &numberwidth
let windowwidth = winwidth(0) - nucolwidth - 3
let foldedlinecount = v:foldend - v:foldstart
" expand tabs into spaces
let onetab = strpart(' ', 0, &tabstop)
let line = substitute(line, '\t', onetab, 'g')
let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
let fillcharcount = windowwidth - len(line) - len(foldedlinecount)
return line . '…' . repeat(" ",fillcharcount) . foldedlinecount . '…' . ' '
endfunction " }}}
set foldtext=MyFoldText()
@thefekete
Copy link

Great function, thanks for posting! Was the cleanest I saw in a search for the best way to modify the foldtext setting.

I found you can get the displayed length of a line (taking into account tabstop) with strdisplaywidth().

So, the following section could be removed:

    " expand tabs into spaces
    let onetab = strpart('          ', 0, &tabstop)
    let line = substitute(line, '\t', onetab, 'g')

And then line 13 would become:

    let fillcharcount = windowwidth - strdisplaywidth(line) - len(foldedlinecount)

Anyways, I know this is a very old gist, but I just thought I'd post here and pass the info back as a thank you for posting your code ;)

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