Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax highlighting in a VIM fold header

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]


like image 800
Mikeage Avatar asked Nov 28 '09 17:11

Mikeage


People also ask

How does Vim do syntax highlighting?

Syntax highlighting is on for vim editor by default. The content of login.sh will be displayed with the following format when the syntax highlighting is on. After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting.

How do I enable code folding in Vim?

z Shift + o opens all folds at the cursor. z c closes a fold at the cursor. z m increases the foldlevel by one. z Shift + m closes all open folds.

How does folding work in Vim?

Marker. Vim folds your code based on characters in the actual text. Usually these characters are put in comments (like // {{{ ), but in some languages you can get away with using something in the language's syntax itself, like { and } in Javascript files.


2 Answers

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.

like image 197
Herbert Sitz Avatar answered Oct 05 '22 13:10

Herbert Sitz


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.

like image 37
mmrobins Avatar answered Oct 05 '22 13:10

mmrobins