Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fold all occurrences of a regex in vim?

Tags:

regex

vim

folding

A colleague of mine is using a horrible source code editor that leaves strange comments all over the code. In Visual Studio, I created a macro, that simply folds all those comment blocks so I don't have to see them.

Now I would like to do the very same thing in vim.

This regex matches every one of those blocks:

/^.*\/\* EasyCODE.*\(\(\n.*\*\/\)\|\(\n.*\/\*.*\)\|\(\n\/\/.*\)\)*/

Now I bet there is a really nice way to fold all matches of this pattern in vim. However, I am rather new to vim and don't know how to do this.

Could you help me there?

Edit: some examples:

These comment blocks always begin with /* EasyCODE. Sometimes the comment have a closing */ right on the end of the first line, sometimes only on the next line. The following lines might or might not contain additional "/* EasyCODE..." blocks.

One of these blocks could look like this

/* EasyCODE ) */
/* EasyCODE ( 0 
some text */
/* EasyCODE F */

or like this

/* EasyCODE V8 */
/* EasyCODE ( 0 */

or like this

/* EasyCODE > */

As I said, the above regex catches them all.

like image 982
bastibe Avatar asked Oct 13 '22 23:10

bastibe


1 Answers

I'm not completely clear on the details of your blocks (hence the comment about providing an example), but you could use something like this:

:set foldmarker=/\*\ EasyCODE,\*/
:set foldmethod=marker

This will configure the 'marker' fold method that looks for certain markers in the code and then it will set the starting marker to /* EasyCODE and the ending marker to */. Hopefully this will give you something to get you started. If you could post a sample source file, I may be able to offer some more advice.

:help folding
:help 'foldmarker'
:help 'foldmethod'
:help fold-marker
like image 149
DrAl Avatar answered Oct 27 '22 00:10

DrAl