views:

437

answers:

2

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]


+3  A: 

I think what you're looking for is changing the foldmethod to indent instead 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.

mmrobins
thanks for the response; perhaps I shouldn't have given that example, though. Your comments are good for C code, but I also often fold text files, using http://www.vim.org/scripts/script.php?script_id=158 . This doesn't seem to lend itself to any solution
Mikeage
I wouldn't think that if you were folding plain text files that syntax highlighting would matter much (I'd be curious to see an example), but if it does the same sort of idea applies. If you start the fold below the line you want to be syntax highlighted, then you should still see the line you want highlighted correctly.
mmrobins
+1  A: 

"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.

Herbert Sitz