vim 中折叠 bash 函数的问题

发布于 2024-07-10 19:13:36 字数 595 浏览 7 评论 0原文

我有一个以函数定义开头的 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 技术交流群。

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

发布评论

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

评论(4

一笑百媚生 2024-07-17 19:13:37

我做了一些研究,找到了解决问题的方法:为了阻止 vim 折叠函数直到文件末尾,我必须在 shExpr 的语法区域中添加一个跳过语句(在文件 sh.vim,通常放置在 /usr/share/vim/vim70/syntax/ 之类的地方):

syn region shExpr ... start="{" skip="^function.*\_s\={" end="}" ...

此更改会阻止语法文件认为 {} 属于 shExpr 组,而它们实际上属于函数组。 无论如何,这就是我的理解。

注意:此修复仅适用于以下语法:

function test
{
....
}

而不适用于此:

function test {
....
}

最后一个错误的快速而肮脏的修复是从 @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 file sh.vim, usually placed somewhere like /usr/share/vim/vim70/syntax/):

syn region shExpr ... start="{" skip="^function.*\_s\={" end="}" ...

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:

function test
{
....
}

and not for this:

function test {
....
}

A quick and dirty fix for the last bug is to remove shExpr from the @shFunctionList cluster.

唔猫 2024-07-17 19:13:37

对于 vim 8.2+,以下内容对我有用:

    syntax enable
    let g:sh_fold_enabled=5
    let g:is_sh=1
    set filetype=on
    set foldmethod=syntax
    " :filteype plugin indent on
    foldnestmax=3 "i use 3, change it to whatever you like.

我将它放在 vimrc 中的哪个位置并不重要。

这将为所有已安装的文件类型打开语法折叠和文件类型插件。

with vim 8.2+ the following worked for me:

    syntax enable
    let g:sh_fold_enabled=5
    let g:is_sh=1
    set filetype=on
    set foldmethod=syntax
    " :filteype plugin indent on
    foldnestmax=3 "i use 3, change it to whatever you like.

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.

心如狂蝶 2024-07-17 19:13:37

它应该可以工作,但语法文件中似乎有一个错误。 折叠区域实际上从单词“function”开始,并尝试继续到结束的“}”,但是“{...}”区域的突出显示接管了结束的“}”,并且折叠继续搜索另一个一。 如果您添加另一个“}”,您可以看到它的实际效果:

function test {
    ...
}
}

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:

function test {
    ...
}
}
妖妓 2024-07-17 19:13:37

Reddit 上似乎有一个更简单的解决方案。

引用作者在帖子中的话:

我使用的选项是:

语法=启用 

  文件类型=sh 

  折叠方法=语法 

  让 g:sh_fold_enabled=3 

  g:is_sh=1 
  

编辑:解决方法

vim -u NONE -c 'let g:sh_fold_enabled=7' -c ':set fdm=syntax' -c 'sy
在'文件.sh

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:

The options I use are:

syntax=enable

filetype=sh

foldmethod=syntax

let g:sh_fold_enabled=3

g:is_sh=1

EDIT: Workaround

vim -u NONE -c 'let g:sh_fold_enabled=7' -c ':set fdm=syntax' -c 'sy
on' file.sh

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.

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