4

我有一个以函数定义开头的 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 {----------------------------------------
...
...
...
~
~

有谁知道解决这个问题的好方法?

4

4 回答 4

1

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.

于 2009-01-15T17:43:06.787 回答
0

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 {
    ...
}
}
于 2009-01-05T21:27:27.033 回答
0

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.

于 2016-08-13T15:30:20.990 回答
0

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.

于 2021-02-03T17:38:03.377 回答