I just joined community, and have a quest:

I would like to show more content in the fold text marker for folded regions in [Neo]VIM (especially for Markdown files). The default fold text shows number of lines folded but I would like to also show number of words and number of characters.


. >>>>>>> ### HeaderText [ 12 lines | 52 words | 256 characters ] >>>>>

I see that there is apparently an option for modifying the fold text. And there are some examples online like this but kinda complicated to follow honestly because was made for a different use case.

To acheive the above would require a way to calculate words and character counts but don't know how to do that in VIM Script or Lua.

  • 1
    There is :help wordcount(), but it's for a buffer.
    – D. Ben Knoble
    Jul 3, 2021 at 18:35

1 Answer 1


I guess I will attempt to answer my own question ... because I think I got an fairly nice looking solution with the first line of the fold text on the left and the line stats (number of lines & character count) aligned to the right of the window.

I skipped doing words because maybe that is less useful (but then again maybe not?)

Turns out there are some things to think about when it comes to what to count and what not to count. I decided to not count the first line which is the markdown level header and minus one to not count the final newline character. I might change my mind about these decisions in future; but good start for now.

Willing to accept someone else's solution instead if someone else comes up with a better one. Thanks!

set foldtext=MyFoldText()
function! MyFoldText()
    let nucolwidth = &fdc + &number*&numberwidth
    let winwd = winwidth(0) - nucolwidth
    let foldlinecount = foldclosedend(v:foldstart) - foldclosed(v:foldstart) + 1
    let foldcharcount = -1
    let lineCounter = 1
    while lineCounter < foldlinecount
      " Increment the count for characters in the range along with newline character
      let foldcharcount += strchars(getline(v:foldstart + lineCounter)) + 1
      let lineCounter += 1
    let prefix = " [ Lines: ".foldlinecount." | Chars: ".foldcharcount." ] "
    let line =  strpart(getline(v:foldstart), 0 , winwd - len(prefix))
    let fillcharcount = winwd - len(line) - len(prefix)
    return line . repeat(" ",fillcharcount) . prefix

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.