8

I noticed that fold texts can show useful information. They usually show something like

+-- 5 lines: <div id="header-inner">--------------------------------------------

Is it possible to change the text in those lines? I noticed that something is possible in foldexpr but would it be possible to completely redesign folds?
e.g.
+ <div id="header-inner"> : "possible comment from line above" : row 27 : length 5

thank you

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
xralf
  • 3,312
  • 45
  • 129
  • 200

2 Answers2

13

There are a few things I don't understand from your question, such as which foldmethod you are using, or what the number of "rows" refers to, but here's a custom foldtext function that should do roughly what you want:

function! MyFoldText()
    let nl = v:foldend - v:foldstart + 1
    let comment = substitute(getline(v:foldstart),"^ *","",1)
    let linetext = substitute(getline(v:foldstart+1),"^ *","",1)
    let txt = '+ ' . linetext . ' : "' . comment . '" : length ' . nl
    return txt
endfunction
set foldtext=MyFoldText()

Explanation:

  1. Find the number of lines contained by the fold.
  2. Get the "comment" from the line before the first folded line (and remove leading spaces).
  3. Get the text from the first line of the fold (and remove leading spaces).
  4. Assemble the above information into the returned foldtext, with appropriate formatting.

Hope this helps. It should be easily tailored to your needs.

Prince Goulash
  • 15,295
  • 2
  • 46
  • 47
  • Thank you. I see that vim is really powerful :-) I'm using foldmethod=expr, but your solution works for every fold method? row should refer to the line number the fold starts. I will try v:lnum – xralf May 13 '11 at 08:11
  • 1
    v:lnum doesn't work but v:foldstart shows the correct line number. There is only little detail, that the end of the string (--------------------------------------------) is not possible to delete. – xralf May 13 '11 at 08:22
  • 1
    Yes, foldtext works for every fold method (I just wasn't if I was reading the `comment` from the correct line, but if it works then there's no problem!). You can get rid of the trailing ----'s by `set fillchars=fold:\ ` (note the trailing space). – Prince Goulash May 13 '11 at 08:42
  • Thats good I'm customizing your code to my need. I tried `set fillchars=fold:\s` but it seems that it's not possible to set whitespace – xralf May 13 '11 at 08:56
  • The fillchars option sets which character is used to fill in the space, so can't use `\s` (which is means "any whitespace"). Is the problem that there is trailing whitespace in the `comment` or `linetext` variables? If so, you could try removing it using something like `let comment = substitute(comment,"\\s*$","",1)` (in addition to the existing substitutions). – Prince Goulash May 13 '11 at 09:11
  • I wanted to achieve something different. Like `set fillchars=fold:' '` , It helped to concatenate `txt` with `' '` (string with about 100 spaces'. I tried it with regex `' {100}'` but this doesn't work. – xralf May 13 '11 at 09:35
  • Why is `1` used as `substitute()` flag? This option doesn't seem to be documented in `:help :s_flags`. It has the same effect as an empty flag though. E.g. these two give the same output: `echo substitute("hello","l","c","")` and `echo substitute("hello","l","c",1)` – builder-7000 Jan 17 '19 at 17:04
2

You can also inspect the folding config from Steve Losh’s bitbucket repo page about vim.

It has very beautiful appearance, which is also very organized!

To see what it is like, you might ckeck it out in this youtube video.

Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • Nice video! I think I have found a new way to organise my Incresing big vimrc file now. –  Mar 14 '17 at 06:52