Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

vim - Change the text in folds

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...