Vim: When folding code allow tabs to remain

2

In vim I would like to change the folding behavior. Suppose I fold this block:

=== fileName ===

        == summary ==

                I think that this defines the class to use for the authentication
                and the parameters to pass the class.

        == tags ==

                <bla bla>
                </bla bla>

It becomes:

=== fileName ===

        == summary ==

+---  2 lines: I think that this defines the class to use for the authentication------------

        == tags ==

                <bla bla>
                </bla bla>

I think that it would be easier to read if it became:

=== fileName ===

        == summary ==

                +---  2 lines: I think that this defines the class to use for the authentication------------

        == tags ==

                <bla bla>
                </bla bla>

(The desired result has an extra tab)

FYI: I have this in my vimrc:

"use indents as the folding method
set foldmethod=indent

"make vim save and load the folding of the document each time it loads
au BufWinLeave * mkview
au BufWinEnter * silent loadview

Update

Upon recommendation from njd I tried setting the foldtext to my own function. I tried both the one that he suggested and the one below. However neither had any effect.

function! MyFoldText()
  return 'johnny'
endfunction

set foldtext=MyFoldText()

What am I missing here?

Note: I also have this: set foldmethod=indent in my .vimrc.

sixtyfootersdude

Posted 2010-02-23T16:29:15.227

Reputation: 6 399

Answers

2

So you want to have the fold message line up with the indent of the folded text?

You need to set the 'foldtext' option to something other than the default foldtext() function.

Something like this:

function! MyFoldText()
  let lines = 1 + v:foldend - v:foldstart
  let spaces = repeat(' ', indent(v:foldstart))

  let linestxt = 'lines'
  if lines == 1
    linestxt = 'line'
  endif

  let firstline = getline(v:foldstart)
  let line = firstline[ind : ind+80]

  return spaces . '+' . v:folddashes . ' ' . lines . ' ' . linestxt . ': ' . line . ' '
endfunction

Then

:set foldtext=MyFoldText()

njd

Posted 2010-02-23T16:29:15.227

Reputation: 9 743

Wow is there really not an easier answer? – sixtyfootersdude – 2010-02-24T14:45:00.797

1Easier like ":set foldmessageplacement=indent" ? No. No such option exists. Just paste that function into your .vimrc, and add the :set command, and you're done. Easy. – njd – 2010-02-24T15:06:34.480

Hey, thanks for the feedback. Put that code into my .vimrc but it had no effect. I modified the function to this:

`function! MyFoldText() return 'hello world' endfunction

set foldtext=MyFoldText()`

But this also had no effect on the folding. Can you offer some clarification as to why this may be having no effect? – sixtyfootersdude – 2010-02-25T15:14:12.007

1My mistake - that "return ..." line should have the correct string concatenator (.) before v:folddashes instead of +. I've corrected my answer. You won't see the change until you reopen and then close a fold. – njd – 2010-02-25T23:43:48.017

Hmm, ok looks like there was a conflict with something else in my vimrc. That fixed it up. However let i += 1 did not work on my machine I needed to use let i = i+1 – sixtyfootersdude – 2010-03-09T22:25:53.440

1I also found that this line: let line = firstline[ind : ind+80] did not work so I changed it for let line = firstline – sixtyfootersdude – 2010-03-09T22:46:32.160