如何在 Vim 中自动折叠很长的 C 代码?

发布于 2024-07-18 08:08:23 字数 60 浏览 5 评论 0原文

我经常遇到没有折叠的 C 代码。 如果没有折叠,阅读它们会很烦人,尤其是长文件。 我怎样才能折叠它们?

I regularly run into C-codes without folding. It is irritating to read them if there is no folding, particularly with long files. How can I fold them?

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

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

发布评论

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

评论(6

羁客 2024-07-25 08:08:23

根据语法折叠

:set foldmethod=syntax

如果您想在要折叠的位上手动执行此操作,

:set foldmethod=manual

请通过选择/移动并按 zf 创建新折叠
例如

shift-v j j zf

(忽略空格)

编辑:另请参阅此答案的注释以了解缩进和标记折叠方法。

To fold according to syntax

:set foldmethod=syntax

If you want to do it manually on the bits you want to fold away

:set foldmethod=manual

then create new folds by selecting / moving and pressing zf
e.g.

shift-v j j zf

(ignoring the spaces)

Edit: Also see the comments of this answer for indent and marker foldmethods.

若沐 2024-07-25 08:08:23

我认为您可能混淆了这些术语。 你需要“包裹”还是“折叠”。 换行是一种换行,通常由于长度而无法在屏幕上显示的行被换行,即在屏幕上显示为几行连续的行(实际上,它是一行,分成几行 - 很难解释,最好在实践)。

在 vim 中,通过设置换行

:set wrap

来打开它,并

:set textwidth=80

确定 vim 应该在哪里换行文本(80 个字符通常是一个不错的措施)。

另一方面,折叠则是完全不同的事情。 vim 将多行代码(例如,函数)折叠成一行代码。 它对于提高代码的可读性很有用。 你可以看到所有这些方法

:help folding

Vim 有几种折叠方法,如果你要找的话, ,我认为是语法折叠,但我可能是错的。 我建议阅读帮助页面,它不长,而且非常有用。

I think you may have mixed the terminology. Do you need "wrapping" or "folding". Wrapping is the one where lines that wouldn't usually fit on screen due to their length, are wrapped, i.e. shown on several consecutive lines on screen (actually, it is one line, in several lines - hard to explain, best to see in practice).

In vim wrapping is set by

:set wrap

to turn it on, and

:set textwidth=80

to determine where vim should wrap the text (80 characters is usually a nice measure).

Folding on the other hand is a completely different matter. It is the one where vim folds several lines of code (for example, a function) into one line of code. It is useful for increasing readability of code. Vim has several folding methods, you can see all of them if you

:help folding

What you are looking for, I think would be, syntax folding, but I could be wrong. I recommend reading the help page, it is not long, and very useful.

自由如风 2024-07-25 08:08:23

其实,还有一个非常直接有效的方法,就是使用foldmethod=marker,并将foldmarker设置为{,}。 那么折叠结果将如下所示:

  1. 所有函数都被折叠。 基本上,它看起来就像IDE中的轮廓。 (如果您不想在开始时折叠所有内容,您也可以设置foldlevel=1或更高)

在此处输入图像描述

  1. 这是打开时正常功能的样子通过 zo 将其设置为 level-1。

输入图片这里的描述

另外,按语法进行折叠需要一些额外的工作,这里有一个很好的 教程。 但我认为用 marker={,} 折叠已经足够了,最重要的是,它简单又整洁。

Actually, there is another very straight forward and effective way, which is using foldmethod = marker and set foldmarker to be {,}. Then the fold result would looks like:

  1. all of the functions fold-ed. Basically, it looks like the outline in IDE. (and you can also set foldlevel=1or more, if you do not want to fold everything at the beginning)

enter image description here

  1. this is what a normal function looks like when you open it with level-1 via zo.

enter image description here

In addition, to do folding by syntax needs a bit of extra work, and here is a good tutorial about it. But I think fold by marker={,} is quite enough, and most importantly, it's simple and neat.

我已经汇总了一个C 和 C++ 的折叠插件。 它超越了语法折叠所做的事情(也许它可以改进,我不知道),并且与缩进和基于标记的折叠相比,留下了更少的噪音和不真正有用的东西。

需要注意的是:为了获得合适的反应时间,我必须进行一些简化,有时结果非常混乱(我们必须输入 zx 来修复它)。

这里有一个小截屏视频,展示该插件如何折叠正确平衡的 C++ 源代码,该源代码当前尚未修改:(

在此处输入图像描述

I've rolled up a fold plugin for C and C++. It goes beyond what is done with syntax folding (may be it could be improved, I don't know), and leaves less noisy and not really useful things unfolded, compared to indentation and marker based folding.

The caveat: in order to have decent reaction times, I had to make some simplifications, and sometimes the result is quite messed-up (we have to type zx to fix it).

Here is a little screencast to see how the plugin folds a correctly balanced C++ source code, which is not currently being modified :(

enter image description here

当爱已成负担 2024-07-25 08:08:23

在 vi(相对于 vim)中,答案是:

:set wm=1

这将换行边距设置为行尾之前的一个字符。 这并不是世界上最好的可变尺寸窗口规格(当难以改变尺寸时,使用绿屏是有意义的)。

这意味着还有另一种方法可以在 vim 中执行此操作:

:set textwidth=30

请参阅: VimDoc 用户手册部分25.1

In vi (as opposed to vim) the answer was:

:set wm=1

This sets the wrap margin to one character before the end of the line. This isn't the world's best specification with variable sized windows (it made sense with green screens when it was hard to change the size).

That means there is also an alternative way to do it in vim:

:set textwidth=30

See: VimDoc User Manual Section 25.1

安静 2024-07-25 08:08:23

您可能需要该设置,

:set foldmethod=syntax

但不要手动输入! 这就错过了 Vims 最大的功能之一,即已经内置了数百种文件类型的自定义设置。 为此,请将其添加到 ~/.vimrc

filetype plugin on
filetype indent on

文件类型检测主要基于扩展名,在本例中为 *.c 文件。 请参阅 :help :filetype 了解更多信息。 您还可以自定义这些基于文件类型的设置。

The you probably want the setting

:set foldmethod=syntax

But don't put that in manually! Thats missing out on one of Vims biggest features which is having custom settings for hundreds of file types already builtin. To get that, add this to your ~/.vimrc

filetype plugin on
filetype indent on

filetype detection is mostly based on extension, in this case *.c files. See :help :filetype for more info. You can also customize these filetype based settings.

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