如何在 Vim 中自动折叠很长的 C 代码?
我经常遇到没有折叠的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据语法折叠
如果您想在要折叠的位上手动执行此操作,
请通过选择/移动并按 zf 创建新折叠
例如
(忽略空格)
编辑:另请参阅此答案的注释以了解缩进和标记折叠方法。
To fold according to syntax
If you want to do it manually on the bits you want to fold away
then create new folds by selecting / moving and pressing zf
e.g.
(ignoring the spaces)
Edit: Also see the comments of this answer for indent and marker foldmethods.
我认为您可能混淆了这些术语。 你需要“包裹”还是“折叠”。 换行是一种换行,通常由于长度而无法在屏幕上显示的行被换行,即在屏幕上显示为几行连续的行(实际上,它是一行,分成几行 - 很难解释,最好在实践)。
在 vim 中,通过设置换行
来打开它,并
确定 vim 应该在哪里换行文本(80 个字符通常是一个不错的措施)。
另一方面,折叠则是完全不同的事情。 vim 将多行代码(例如,函数)折叠成一行代码。 它对于提高代码的可读性很有用。 你可以看到所有这些方法
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
to turn it on, and
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
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.
其实,还有一个非常直接有效的方法,就是使用
foldmethod=marker
,并将foldmarker
设置为{,}
。 那么折叠结果将如下所示:设置foldlevel=1
或更高)zo
将其设置为 level-1。另外,按语法进行折叠需要一些额外的工作,这里有一个很好的 教程。 但我认为用
marker={,}
折叠已经足够了,最重要的是,它简单又整洁。Actually, there is another very straight forward and effective way, which is using
foldmethod = marker
and setfoldmarker
to be{,}
. Then the fold result would looks like:set foldlevel=1
or more, if you do not want to fold everything at the beginning)zo
.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 :(
在 vi(相对于 vim)中,答案是:
这将换行边距设置为行尾之前的一个字符。 这并不是世界上最好的可变尺寸窗口规格(当难以改变尺寸时,使用绿屏是有意义的)。
这意味着还有另一种方法可以在 vim 中执行此操作:
请参阅: VimDoc 用户手册部分25.1
In vi (as opposed to vim) the answer was:
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:
See: VimDoc User Manual Section 25.1
您可能需要该设置,
但不要手动输入! 这就错过了 Vims 最大的功能之一,即已经内置了数百种文件类型的自定义设置。 为此,请将其添加到 ~/.vimrc
文件类型检测主要基于扩展名,在本例中为 *.c 文件。 请参阅 :help :filetype 了解更多信息。 您还可以自定义这些基于文件类型的设置。
The you probably want the setting
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 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.