1

When a block of text is folded in vim, the first line of folded text is displayed in the foldline at a constant offset from the left edge of the window. Can the offset of that text be instead set to be what it was before folding?

For example, this input file:

$ cat -T <foo
1
^I01
^I^I001
^I^I^I0001
^I^I^I^I00001
^I^I^I^I00002
^I^I^I^I00003
^I^I^I^I00004
^I^I^I^I00005
^I^I^I0002
^I^I002
^I02
2

# vim: list lcs=tab\:│\  foldmethod=indent foldlevel=10

appears as follows in vim:

$ vim -u NONE -N foo
1
│       01
│       │       001
│       │       │       0001
│       │       │       │       00001
│       │       │       │       00002
│       │       │       │       00003
│       │       │       │       00004
│       │       │       │       00005
│       │       │       0002
│       │       002
│       02
2

# vim: list lcs=tab\:│\  foldmethod=indent foldlevel=10

(The original file can be regenerated by copying and pasting the cat -T output from above and piping through sed 's/\^I/\t/g'.)

If the deepest indentation level is folded, it looks like:

1
│       01
│       │       001
│       │       │       0001
+-----  5 lines: 00001------------------------------------
│       │       │       0002
│       │       002
│       02
2

How can the text in the foldline be positioned with its original offset with respect to the left edge of the window? Can the original listchars be made to appear in the foldline as well? This would look like the following (the final listchar was replaced by a '+'):

1
│       01
│       │       001
│       │       │       0001
│       │       │       +-------00001---------------------
│       │       │       0002
│       │       002
│       02
2

Finally, is it possible for the background color of the foldline to extend only from the '+' to the right edge of the window?

1

The option foldtext defines the function to call, to determine the text displayed for folded lines.

You could start by adding the following to your vimrc:

function MyFoldText()
    let indent = indent(v:foldstart) - &sw
    return repeat(' ', indent) . "+" . repeat('-', &sw-2) . ' ' . trim(getline(v:foldstart))
endfunction

set foldtext=MyFoldText()

Rework the function MyFoldText() to your liking.

See :help foldtext and :h foldtext().

Note, that this might be slower than the native implementation of foldtext().

| improve this answer | |
  • That's very impressive, thanks. – user001 Jun 10 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.