Question

Is there any way to have VIM continue to apply formatting to the line used as the header for a fold?

E.g., I have the following code:

int foo(int a, int b) {
    int c;
....
}

When folded, I see:

+-- 4 lines: int foo(int a, int b) {----------------------------

However, the whole line is highlighted as per the "Folded" class. Is there any way to disable this, so I continue to see the syntax highlighting?

[for a simple example this is not so important, but I also use folding extensively in viewing large data files, and there the formatting is much more important to me]


Was it helpful?

Solution

However, the whole line is highlighted as per the "Folded" class. Is there any way to disable this, so I continue to see the syntax highlighting?"

No, the folded headline text is not text that is part of a file and is not directly editable, just calculated and overlaid on the screen. The 'Folded' highlight is applied to the entire line, and all folds have same highlighting applied.

For my use with vim as an outliner (in the VimOutliner project) I hacked the vim source to allow for different fold highlighting depending on the fold level, so there are multiple fold heading highlights that are applied (e.g. FoldLevel1, FoldLevel2, etc.). I assume it could be further hacked to use already-existing highlighting of the text at the fold head line, but given the way folding works I suspect that might be harder to do than it sounds.

Sorry, just struck me that the suggestion for foldmethod of indent may be exactly what you're looking for anyway, which does preserve syntax in the unindented lines while still having them function as a sort of heading for the folded section.

OTHER TIPS

I think what you're looking for is changing the foldmethod to indent instead of manual, which is the default. Type this in command mode.

:set foldmethod=indent

Now if you go inside your foo function and type zm (increase fold level by one), it will look like this:

int foo(int a, int b) {
+--  2 lines: int c;------------------------------------------------------------
}

The foo line will still have syntax highlighting. To unfold of course, type zr. For convenience I put the following few lines in my .vimrc to quickly fold or unfold everything:

" Folding and unfolding
map ,f :set foldmethod=indent<cr>zM<cr>
map ,F :set foldmethod=manual<cr>zR<cr>

There's a also a good tutorial here, and of course reading the vim help on foldmethod might lead you to other methods that you like better than indent, but the way your example code looks you'll probably want indent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top