如何在 Vim 中一次性折叠整个文件到特定级别?
我想在 Vim 中按一个按钮并折叠所有代码,以便只显示达到特定(和可变)缩进级别的代码。例如,当我只想查看方法名称而不是其缩进例程时,这非常有用。
“Vim: 仅折叠顶层折叠”问题有一个解决方案缩进级别,但每次更改级别时都需要设置环境。
当我的光标位于缩进级别(例如级别 2)时,我希望整个文件在所有方法中都折叠到该缩进级别。
这是 Vim 中内置的吗?有谁知道有一个好的插件可以做到这一点吗?
I want to push one button in Vim and fold all the code so only code up to a specific (and variable) indent level is showing. Very useful when I want to only see method names for example and not their indented routines.
The “Vim: Fold top level folds only” question has a solution to an indent level, but it requires an environment set each time you change levels.
When my cursor is at an indent level (say level 2), I want the entire file to fold to that indent level across all methods.
Is this built into Vim somewhere? Does anyone know of a good plugin that does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将折叠配置为通过缩进定义:
并尝试以下命令:
要快速访问此命令,请为其创建映射,如下所示:
Configure folding to be defined by indentation:
and try the following command:
To quickly access this command, create a mapping for it as follows:
不需要插件,它内置在 Vim 中。
“foldlevel”(或更短的“fdl”)和“foldnestmax”(“fdn”)似乎就是我们正在寻找的。您只需在 .vimrc 文件中设置“foldmethod”(或更短的“fdm”)和“foldnestmax”(或“fdn”):
或较短的版本:
然后您可以使用直接命令更改折叠级别:zm或zr。
No need of a plugin, it is builtin in Vim.
'foldlevel' (or shorter 'fdl') and 'foldnestmax' ('fdn') seems to be what we were looking for. You only have to set the 'foldmethod' (or shorter 'fdm') and a 'foldnestmax' (or 'fdn') in you .vimrc file:
OR the shorter version:
Then you can change the fold level with direct commands: zm or zr.
因为当foldmethod为expr时,foldnestmax不适用,所以当我遇到你的问题时,我寻找了其他东西。这是我想到的,毫无疑问可以改进:
我更喜欢编号的地图,但对于您的基于当前行的缩进,大致如下:
Because foldnestmax doesn't apply when foldmethod is expr, I looked for something else when I came across your question. Here is what I came up with, which doubtless can be improved:
I prefer the numbered maps, but for yours based on indentation of the current line, something along these lines: