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 :

Folding

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

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
VIM Better FoldText, 5.0 out of 5 based on 1 rating

13 Comments

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

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • I am using Powerline which provides that cool bottom status bar for both tmux and vim, in this screenshot you’re seeing vim inside a tmux window.

      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  2. Thanks :)

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  3. 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?

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • Sorry for self-replying but I found out why – there was a typo in your code. The &fillchar on line 5 should be replaced with &fillchars. :)

      VA:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
      • Ahh thanks a bunch for that correction, an embarrassing typo, a result of not copy pasting :)

        VN:F [1.9.22_1171]
        Rating: 0 (from 0 votes)
  4. 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!

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  5. 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

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  6. This is so much more refreshing to look at than the default thanks for the Vim script.

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  7. Thanks for sharing with us! I was wondering if you could be enable to integrate the percent function into your code, like this: http://www.gregsexton.org/2011/03/improving-the-text-displayed-in-a-fold/

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

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a Reply