Problem with bash folding function in vim

I have a bash script file that starts with a function definition, for example:

#!/bin/bash # ..... # ..... function test { ... ... } ... ... 

I am using vim 7.2 and I set g:sh_fold_enabled=1 so that bending is enabled using bash. The problem is that the bending of the functional test is not finished correctly, that is, it continues to the end of the file. It looks something like this:

 #!/bin/bash # ..... # ..... +-- 550 lines: function test {---------------------------------------- ~ ~ 

The function itself is about 40 lines, and I want it to look like this ("images" say more than a thousand words, say ...):

 #!/bin/bash # ..... # ..... +-- 40 lines: function test {---------------------------------------- ... ... ... ~ ~ 

Does anyone know a good solution to this problem?

+4
source share
3 answers

I did some research and found a way to fix the problem: To stop vim from bending to the end of the file, I had to add the skip instruction for shExpr to the shExpr (in the sh.vim file, usually placed somewhere like /usr/share/vim/vim70/syntax/ ):

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

This change stops the syntax file, believing that { and } belongs to the shExpr group when they really belong to the function group. Somehow I understood that.

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 error is to remove shExpr from the @shFunctionList cluster.

0
source

It should just work, but there seems to be an error in the syntax file. The bend area actually starts with the word “function” and tries to continue closing “}”, but the selection for the area “{...}” takes over the closing “}”, and the folding continues while looking for another one. If you add another '}', you can see this in action:

 function test { ... } } 
0
source

There seems to be a simpler solution on Reddit .

To quote the author in the message:

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 was apparently consistent at the crease level in the discussion. This solution works great for me. I did not need to edit the syntax file.

Edit: g: sh_fold_enabled = 5 is actually correct. Not 4. In addition, as the poster showed Reddit, these commands should go to any other setting in vimrc, except for plugins.

0
source

All Articles