当同一行有一个 { 和一个 } 时,Vim 中出现奇怪的折叠。&*
我目前正在编辑一个带有 twp 函数的 C++ 文件,类似于
int func_name_1(int param) {
do_stuff();
and_more();
}
int func_name_2(int param) {
do_different_stuff();
STRUCT_TYPE s = {5, 8, 10, 12};
do_something_with(s);
}
If I do a zc
on the word func_name_1
, vim 会按预期折叠该函数,因此它现在看起来像
int func_name_1(int param) {--------------------
然而,在 func_name_2 上,函数的折叠方式如下:
int func_name_2(int param) {---------------------
do_something_with(s);
}
这并不完全是我想要的。由于某种原因,一行上的打开和关闭 {} 似乎会干扰折叠算法。
如果有人知道我如何获得“更好”的折叠,我将不胜感激任何正确方向的暗示。
编辑我认为与此问题相关的选项在我的缓冲区中设置,如下
set foldmarker={,}
set foldmethod=marker
set foldtext=getline(v:foldstart)
编辑 II:如果我更改带有开始和结束 {...} 的行到 STRUCT_TYPE s = { 5, 8, 10, 12};
(注意 {
后面的空格),然后折叠将按预期进行。
I am currently editing a C++ file with twp function similar to
int func_name_1(int param) {
do_stuff();
and_more();
}
int func_name_2(int param) {
do_different_stuff();
STRUCT_TYPE s = {5, 8, 10, 12};
do_something_with(s);
}
If I do a zc
on the word func_name_1
, vim folds the function, as expected, so that it now looks like
int func_name_1(int param) {--------------------
On func_name_2
however, the function is folded like so:
int func_name_2(int param) {---------------------
do_something_with(s);
}
which is not exactly what I want. For some reason the opening and closing {} on one line seem to disturb the folding algortithm.
If someone knows how I can get a "better" folding, I'd appreciate any hinter into the right direction.
Edit the options that I believe are relevant for this problem are set in my buffer like so
set foldmarker={,}
set foldmethod=marker
set foldtext=getline(v:foldstart)
Edit II: If I change the line with the opening and closing {...} to STRUCT_TYPE s = { 5, 8, 10, 12};
(Note the space after the {
), then the folding works as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦....我明白了....
:helpfold-marker
告诉我:标记可以包含一个级别 [..] 以下数字指定折叠level
因此,紧随
{
之后的5
指定了折叠级别,这把事情弄乱了。Oh.... I see....
:help fold-marker
tells me: Markers can have a level included [..] The following number specifies the foldlevel
So, the
5
immediatly after the{
specified the fold level, which messed things up.zc
命令关闭当前折叠。您可能创建了一个能够关闭它的文件夹,因此您的问题在于创建折叠而不是关闭它。要创建折叠,请使用
zf
或zF
。zf
适用于视觉模式,也适用于zf2j
之类的运动,以创建向下 2 行的折叠。zF
已经以“逐行”方式工作。对于您的情况,最实用的方法是将光标定位在括号上(例如使用
f{
)并运行zf%
创建并关闭一个折叠,该折叠转到匹配的括号。无论如何,如果该括号位于另一行(因此您可以使用]]
转到它),那会更实用,但这是超出本答案范围的约定:-)The
zc
command closes the current fold. You may have a folder created to be able to close it, so your problem was on creating the fold and not closing it.To create the fold use
zf
orzF
.zf
Works on visual mode, and also on a motion likezf2j
to create a fold on 2 lines down.zF
already works in a "line-wise" way.The most practical way, in your case, would be to position the cursor on the bracket (using
f{
for example) and runzf%
to create and close a fold that goes to the matching bracket. Anyway, it would be even more practical if that bracket was on another line (so you could use]]
to go to it) but that's a convention out of the scope of this answer :-)