vim 中折叠 bash 函数的问题
我有一个以函数定义开头的 bash 脚本文件,如下所示:
#!/bin/bash
# .....
# .....
function test {
...
...
}
...
...
我使用 vim 7.2,并且设置了 g:sh_fold_enabled=1
以便使用 bash 启用折叠。 问题是功能测试的折叠没有正确结束,即它一直持续到文件末尾。 它看起来像这样:
#!/bin/bash
# .....
# .....
+-- 550 lines: function test {----------------------------------------
~
~
函数本身只有大约 40 行,我想要看起来像这样的东西(他们说“图像”比一千个单词说得更多......):
#!/bin/bash
# .....
# .....
+-- 40 lines: function test {----------------------------------------
...
...
...
~
~
有谁知道这个问题的好解决方案?
I have a bash script file which starts with a function definition, like this:
#!/bin/bash
# .....
# .....
function test {
...
...
}
...
...
I use vim 7.2, and I have set g:sh_fold_enabled=1
such that folding is enabled with bash. The problem is that the folding of the function test is not ended correctly, i.e. it lasts until the end of file. It looks something like this:
#!/bin/bash
# .....
# .....
+-- 550 lines: function test {----------------------------------------
~
~
The function itself is just about 40 lines, and I want something that lookes like this ("images" say more than a thousend words, they say...):
#!/bin/bash
# .....
# .....
+-- 40 lines: function test {----------------------------------------
...
...
...
~
~
Does anyone know a good solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我做了一些研究,找到了解决问题的方法:为了阻止 vim 折叠函数直到文件末尾,我必须在
shExpr
的语法区域中添加一个跳过语句(在文件sh.vim
,通常放置在/usr/share/vim/vim70/syntax/
之类的地方):此更改会阻止语法文件认为
{
和}
属于 shExpr 组,而它们实际上属于函数组。 无论如何,这就是我的理解。注意:此修复仅适用于以下语法:
而不适用于此:
最后一个错误的快速而肮脏的修复是从 @shFunctionList 集群中删除 shExpr。
I have done some research, and found a way to fix the problem: To stop vim from folding functions until the end of file, I had to add a skip-statement to the syntax region for
shExpr
(in the filesh.vim
, usually placed somewhere like/usr/share/vim/vim70/syntax/
):This change stops the syntax file from thinking that the
{
and}
belongs to the shExpr group, when they actually belong to the function group. Or that is how I have understood it, anyway.Note: This fix only works for the following syntax:
and not for this:
A quick and dirty fix for the last bug is to remove shExpr from the @shFunctionList cluster.
对于 vim 8.2+,以下内容对我有用:
我将它放在 vimrc 中的哪个位置并不重要。
这将为所有已安装的文件类型打开语法折叠和文件类型插件。
with vim 8.2+ the following worked for me:
it did not matter where i put it in my vimrc.
and this turns on syntax folding and the file type plugin for all installed file types.
它应该可以工作,但语法文件中似乎有一个错误。 折叠区域实际上从单词“function”开始,并尝试继续到结束的“}”,但是“{...}”区域的突出显示接管了结束的“}”,并且折叠继续搜索另一个一。 如果您添加另一个“}”,您可以看到它的实际效果:
It should just work, but there seems to be a bug in the syntax file. The fold region actually starts at the word 'function' and tries to continue to the closing '}', but the highlighting for the '{...}' region takes over the closing '}' and the fold continues on searching for another one. If you add another '}' you can see this in action:
Reddit 上似乎有一个更简单的解决方案。
引用作者在帖子中的话:
g:sh_fold_enabled=4 似乎是讨论中商定的折叠级别。 这个解决方案对我来说非常有效。 我不必编辑语法文件。
编辑: g:sh_fold_enabled=5 实际上是正确的。 不是 4。
此外,正如 Reddit 上的海报所示,这些命令必须先于 vimrc 中的任何其他设置(插件除外)。
There seems to be a simpler solution on Reddit.
To quote the author in the post:
g:sh_fold_enabled=4 seemed to be the agreed upon fold-level in the discussion. This solution is working perfectly for me. I did not have to edit the syntax file.
Edit: g:sh_fold_enabled=5 is actually the right one. Not 4.
Also, as the poster showed on Reddit, those commands must go before any other setting in vimrc, except the plugins.