I’d like to set two methods for folding
:set foldmethod=indentand retain all its features-
hiding comments with
:set foldmethod=marker:set foldmarker=/*,*/
I found out that this is not possible. Is it possible to achieve the desired folding and set this in .vimrc or to use some script or plugin for this?
It’s not possible to have different foldmethod types in same buffer. How is Vim to know that there are some comments at same indent level as other text that you want to treat as being of a different (higher numbered) level?
I’m sure you can achieve what you want by setting foldmethod to ‘expr’. This is most flexible way of doing folds in Vim but can get complicated (and/or slow) depending on what you want. I think it would work fairly easily for your use case, though.
First, somewhere in your vimrc or vimscripts you need to make sure that foldexpr is getting defined for the filetype in question.
and you then have to flesh out your foldexpr function so that it assigns levels in a way that results in behavior you want. Something like the code below might come close to working in cases where each comment line has prefix symbol (i.e., not in your case), but I expect it needs some tweaks.
h: fold-exprwould be a good place to look for help:Would need to be modified to assign higher level for lines between comment start and end markers the way you want:
I don’t expect the foldmethod functions I’ve written would work exactly as written. But they do point the way to something that would work.
Note that use of level of ’20’ for comments is just arbitrary level that allows them to be folded while all (presumably lower-leveled) indented code could be visible. ’21’ for last line of comment section is just to differentiate it from the previous lines of comments that have level of 20, to know that next line should be treated as regular line of code.
Also, key ops like ‘zc’ and ‘zo’ will not work quite right on comments when they’re set to level much higher than surrounding code. Would want to use direct command like
:set foldlevel=21to show all comment lines.Not pretty, and I expect it could be simplified a little, but something like this is what I think is required for what you want.
Actually, thinking through this a little more, I think you would want the first line of any comment blocks to be at same level as if it were a non-comment line, only subsequent comment lines in same block would need to be of higher level to have them “fold” into the starting comment line. In the code I gave, if it works or comes close to working at all, I think vim would fold all the comment lines behind the preceding non-comment line, which isn’t what you want, but I unfortunately don’t have more time to devote to this little puzzle. . . I’ve done this sort of custom-folding quite a few times and generally always have a little bit of trial and error in getting exactly what I want.