Is it possible to have vim highlight folds differently depending on whether every line in a fold is only comments or whitespace? In that case, how?
You cannot change the fold highlighting, as there's only one highlight group for all folds (
:help hl-Folded). What you can do is indicate the "all comments"-nature of a fold via a custom
fold-foldtext; for example, you could prepend
COMMENTED:. It would then look like this:
+-- 3 lines: # some stuff here COMMENTED: +-- 3 lines: # comment command 2 command 3
The second part of a solution is determining whether all lines inside a fold are actually comments. This could be done by querying the syntax highlighting (via
synID()), which would be universal, but also cumbersome and potentially slow. If all you care about is fully commented lines with a comment prefix, you could obtain that from
'comments' for the current filetype and manually match against it. (My ingo-library plugin has a function for that.) If all you care about is a simple, generic comment prefix like
# (as used in shell scripts, Perl, etc.), a simple pattern match will do, like this:
function! CommentIndicator() for l:lnum in range(v:foldstart, v:foldend) if getline(l:lnum) !~# '^\s*#' " This fold is not fully commented. return '' endif endfor return 'COMMENTED: ' endfunction let &foldtext = 'CommentIndicator() . foldtext()'
Yes, it is. Actually, you can use
As an example:
set foldmethod=expr foldexpr=getline(v:lnum)=~'^\s*'.&commentstring
You can see full answer with similiar problem here: .