VIM Better FoldText

Update 3: Improved the code for extraction of fillchar from ‘fillchars’ option and taking ‘foldcolumn’ into account.

Update 2: Fixed the regex for removing the fold marker from the displayed text by splitting the ‘{{{‘ in the regex to ‘{{‘. ‘{‘ so that it is not treated as a fold marker by vim. Thanks to Amadan on reddit here.

Update: Fixed ‘NeatFoldText()’ to get foldchar from ‘fillchars’ settings.

Initially when I started using VIM, I had borrowed configs from my mentor Vagmi Mudumbai (to whom I am eternally thankful!!!).

He used folding in VIM actively. I on the other hand though, barely got around using VIM itself, found folding to be quite confusing and hard to work with. The first folding mapping I learnt was ‘zR’, which is for recursively opening all folds in the current documents, so I don’t have to deal / worry about them. I became so habitual to using ‘zR’ that one day I decided I was better off without folding altogether, after all I was clearly not using folding anyways. That day I removed folding related configs from my ~/.vimrc. Much later as I became more comfortable using VIM, I came back to folding and have not looked back ever since!

I have always thought though that the vim’s default folding look and feel could use some beautification, and although I have been aware about ‘foldtext’, I never really got around to understanding how to take advantage of it to customize things like I wanted.

After quite a bit of effort and quite a few iterations, I now present to you my ‘NeatFoldText()’, a vim function that makes the fold line look really impressive, meaningful and neat at the same time! It puts focus on what’s most important (the text in the start of the fold). And right aligns the number of lines in the fold. Also formats it to fixed size which makes it look neat and consistent.

Put this in your $VIMRC (~/.vimrc) :

And this is how the fold text look like :


This example should also prove to be a great starting point / example for those who wish to tweak the folding text further.


 Add your comment
  1. What’s that stuff / plugins at the bottom of the screen, look cool

  2. After pasting your code into my .vimrc, whenever I fold text the output is 0——– with dashes across the whole window width. What could be wrong here? Any hint?

  3. I truly love your site.. Great colors & theme. Did you build this site yourself?
    Please reply back as I’m looking to create my own website and want to learn where you got this from or just what the theme is named. Kudos!

  4. I really like your blog.. very nice colors & theme. Did you design this
    website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to construct my own blog and would like to know where u got this from. kudos

  5. This is so much more refreshing to look at than the default thanks for the Vim script.

  6. Thanks for sharing with us! I was wondering if you could be enable to integrate the percent function into your code, like this:

    I would like to have same style as your code, but with the percentage indicators. Is that possible too?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.