如何折叠 vim 中所有出现的正则表达式?

发布于 2024-09-24 10:30:53 字数 891 浏览 1 评论 0原文

我的一位同事正在使用一个可怕的源代码编辑器,它会在代码中留下奇怪的注释。在 Visual Studio 中,我创建了一个宏 ,这只是折叠所有这些评论块,这样我就不必看到它们。

现在我想在 vim 中做同样的事情。

这个正则表达式匹配这些块中的每一个:

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

现在我打赌有一种非常好的方法可以在 vim 中折叠该模式的所有匹配项。然而,我对 vim 还很陌生,不知道该怎么做。

你能在那里帮我吗?

编辑:一些示例:

这些注释块始终以 /* EasyCODE 开头。有时注释在第一行末尾有一个结束 */,有时仅在下一行。以下行可能包含也可能不包含其他“/* EasyCODE...”块。

这些块之一可能看起来像这样

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

或像这样

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

或像这样

/* EasyCODE > */

正如我所说,上面的正则表达式捕获了所有这些块。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2024-10-01 10:30:53

我不完全清楚你的块的细节(因此关于提供示例的评论),但你可以使用这样的东西:

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

这将配置“标记”折叠方法,该方法在代码中查找某些标记,然后它会将起始标记设置为 /* EasyCODE 并将结束标记设置为 */。希望这会给您一些帮助您入门。如果您可以发布示例源文件,我也许可以提供更多建议。

:help folding
:help 'foldmarker'
:help 'foldmethod'
:help fold-marker

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文